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

C#清除字符串内空格的方法

时间:2017-11-29 14:50:40      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:bottom   post   UI   hit   nbsp   程序设计   数据   text   method   

本文实例讲述了C#清除字符串内空格的方法,分享给大家供大家参考。具体如下:

关键代码如下:


代码如下:

/// <summary>

/// 清除字符串内空格

/// </summary>

/// <param name=”str”>需要处理的字符串</param>

/// <returns>处理好后的字符串</returns>

public static string ExceptBlanks(this string str)

{

int _length = str.Length;

if (_length > 0)

{

StringBuilder _builder = new StringBuilder(_length);

for (int i = 0; i < str.Length; i++)

{

char _c = str[i];

//switch (_c)

//{

//    case ‘\r’:

//    case ‘\n’:

//    case ‘\t’:

//    case ‘ ‘:

//        continue;

//    default:

//        _builder.Append(_c);

//        break;

//}

if (!char.IsWhiteSpace(_c))

_builder.Append(_c);

}

return _builder.ToString();

}

return str;

}

测试代码如下:


代码如下:

[TestMethod()]

public void ExceptBlanksTest()

{

string str = @”20140901  11 22 33  “; // TODO: 初始化为适当的值

string expected = “20140901112233”; // TODO: 初始化为适当的值

string actual = StringToolV2.ExceptBlanks(str);

Assert.AreEqual(expected, actual);

}

测试结果如下图所示:

技术分享图片

希望本文所述对大家的C#程序设计有所帮助

除声明外,跑步客文章均为原创,转载请以链接形式标明本文地址
  C#清除字符串内空格的方法

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






相关内容

C#清除字符串内空格的方法

标签:bottom   post   UI   hit   nbsp   程序设计   数据   text   method   

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

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