当前位置: 移动技术网 > IT编程>开发语言>c# > C#实现移除字符串末尾指定字符的方法

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

2019年07月18日  | 移动技术网IT编程  | 我要评论

字符串的操作是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);
}

测试结果如下图所示:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网