码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2017-11-13 23:06:34      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:top   ref   poi   tchar   http   center   str   .com   无法   

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

测试结果如下图所示:

技术分享

除声明外,跑步客文章均为原创,转载请以链接形式标明本文地址
  C#实现移除字符串末尾指定字符的方法

本文地址:  http://www.paobuke.com/develop/c-develop/pbk23646.html






相关内容

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

标签:top   ref   poi   tchar   http   center   str   .com   无法   

原文地址:http://www.cnblogs.com/paobuke/p/7828166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!