欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#清除字符串內(nèi)空格的方法

 更新時(shí)間:2014年10月12日 16:48:05   投稿:shichen2014  
這篇文章主要介紹了C#清除字符串內(nèi)空格的方法,是C#操作字符串非常實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#清除字符串內(nèi)空格的方法,分享給大家供大家參考。具體如下:

關(guān)鍵代碼如下:

復(fù)制代碼 代碼如下:
/// <summary>
/// 清除字符串內(nèi)空格
/// </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;
}

測試代碼如下:

復(fù)制代碼 代碼如下:
[TestMethod()]
public void ExceptBlanksTest()
{
    string str = @"20140901  11 22 33  "; // TODO: 初始化為適當(dāng)?shù)闹?br />     string expected = "20140901112233"; // TODO: 初始化為適當(dāng)?shù)闹?br />     string actual = StringToolV2.ExceptBlanks(str);
    Assert.AreEqual(expected, actual);
}

測試結(jié)果如下圖所示:

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助

相關(guān)文章

最新評論