字符串的操作是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); }
测试结果如下图所示:
如对本文有疑问, 点击进行留言回复!!
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
C#实现获取本地内网(局域网)和外网(公网)IP地址的方法分析
网友评论