首页 > 编程 > C# > 正文

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

2020-01-24 02:30:50
字体:
来源:转载
供稿:网友

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

关键代码如下:

/// <summary>/// 移除字符串末尾指定字符/// </summary>/// <param name="str">需要移除的字符串</param>/// <param name="value">指定字符</param>/// <returns>移除后的字符串</returns>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);}

测试结果如下图所示:

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表