从字符串中修剪最后一个字符

编程入门 行业动态 更新时间:2024-10-27 06:22:31
本文介绍了从字符串中修剪最后一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个字符串说

你好!世界!

我想做一个微调或删除取出!过世界,但不脱你好。

解决方案

世界,你好!TrimEnd('!')。

了解更多

编辑:

我已经注意到这种类型的相当多每个人都建议删除指定字符串的最后一个字符的问题。但是,这并不符合修剪方法的定义。

  

修剪 - 删除所有出现   从空格字符   开始与该实例的结束。

MSDN剪裁

根据这一定义从字符串只删除最后一个字符是不好解决。

因此​​,如果我们想修剪从字符串最后一个字符我们应该做这样的事

实例作为扩展方法:

公共静态类MyExtensions {   公共静态字符串TrimLastCharacter(此字符串str)   {      如果(String.IsNullOrEmpty(STR)){         返回海峡;      } 其他 {         返回str.TrimEnd(STR [str.Length - 1]);      }   } }

I have a string say

"Hello! world!"

I want to do a trim or a remove to take out the ! off world but not off Hello.

解决方案

"Hello! world!".TrimEnd('!');

read more

EDIT:

What I've noticed in this type of questions that quite everyone suggest to remove the last char of given string. But this does not fulfill the definition of Trim method.

Trim - Removes all occurrences of white space characters from the beginning and end of this instance.

MSDN-Trim

Under this definition removing only last character from string is bad solution.

So if we want to "Trim last character from string" we should do something like this

Example as extension method:

public static class MyExtensions { public static string TrimLastCharacter(this String str) { if(String.IsNullOrEmpty(str)){ return str; } else { return str.TrimEnd(str[str.Length - 1]); } } }

更多推荐

从字符串中修剪最后一个字符

本文发布于:2023-11-06 21:56:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564779.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字符

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!