C#实现移除字符串末尾指定字符的方法

这篇文章主要介绍了C#实现移除字符串末尾指定字符的方法,十分常见且重要的一个应用技巧,需要的朋友可以参考下

字符串的操作是C#程序设计中十分重要的一个组成部分,本文就以实例形式展现了C#实现移除字符串末尾指定字符的方法。相信对大家学习C#程序设计有一定的借鉴价值。

关键代码如下:

 ///  /// 移除字符串末尾指定字符 ///  /// 需要移除的字符串 /// 指定字符 /// 移除后的字符串 public static string RemoveLastChar(this string str, string value) { int _finded = str.LastIndexOf(value); if (_finded != -1) { return str.Substring(0, _finded); } return str; } 

单元测试如下:

 [TestMethod()] public void RemoveLastCharTest() { string _testString = "YANZHIWEI。"; string _expected = "YANZHIWEI"; string _actual = StringToolV2.RemoveLastChar(_testString, "。"); Assert.AreEqual(_expected, _actual); } 

测试结果如下图所示:

以上就是C#实现移除字符串末尾指定字符的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程