当前位置: 编程技术>.net/c#/asp.net
C#实现移除字符串末尾指定字符的方法
来源: 互联网 发布时间:2014-11-02
本文导语: 字符串的操作是C#程序设计中十分重要的一个组成部分,本文就以实例形式展现了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);
}
测试结果如下图所示: