C# 中奇妙的函数–8. String Remove() 和 Replace()

当对字符串进行操作时,我们经常要删除或者是替换一部分子字符串。 Remove() 和 Replace() 两个函数在这种情况就派上用场了。

Remove() – 删除一部分子字符串

我们都知道 substring 可以返回字符串的一部分。 当我们想保留字符串中的一部分substring丢弃其它部分时,就可以使用substring,当我们想删除字符串的一部分并保留其它部分时,就使用Remove。

Remove 有两种形式:

  • Remove(int startIndex)
    • 删除此字符串中从指定位置到最后位置的所有字符。
  • Remove(int startIndex, int length)
    • 从此实例中的指定位置开始删除指定数目的字符。

Remove 会进行区域的检查,

对于第一种形式 当

1. startIndex 小于零 或

2. startIndex 指定的位置不在此字符串内;

对于第二种形式当

1. startIndex 或 count 小于零 或

2. startIndex 加 count 之和指定一个此实例外的位置。

会抛出异常。

 

在Remove() 和 substring()两者中,选择哪一个应该是很容易的,看下面的实例:



string test = "Now is the time for all good men to come to the aid of their country.";



// 取头 10 个字符

var sliceUsingSubstring = test.Substring(0, 10);



// 删除第10个字符后的所有字符

var sliceUsingRemove = test.Remove(10);
如果我们想获取前面10个字符,两种方法都是可以用的,但是Remove好像稍微简洁一点,因为你不需要指点起始点.
那么如果你想获取最后的10个字符呢?

string test = "Now is the time for all good men to come to the aid of their country.";


// 获取从 length - 10 到最后的部分.

var sliceUsingSubstring = test.Substring(test.Length - 10);



// 删除从头到 length - 10, 其余留下

var sliceUsingRemove = test.Remove(0, test.Length - 10);

在这种情况下,substring 看上去更加的简洁了。

现在看来,它们的确是八斤八两,但是.net 在这一类的情况下提供了两种方式,可以完全凭你的喜好来使用。

 

但是当我们要保留或者删除字符串中间一段时,就可以看出它们的不同了:


// 取从第 10 个开始长度为 10 的子串

var sliceUsingSubstring = test.Substring(10, 10);

// 删除从第20个字符之后的部分, 再删除头 10个

var sliceUsingRemove = test.Remove(20).Remove(0, 10);

 

在这种情况下,很明显substring简便易读,而且只需要一次操作。

 

但是如果我们想要删除中间的一段字符串:

 

// 取从0开始,长度为10的子串, 再取从第20 开始到末尾的部分

var sliceUsingSubstring = test.Substring(0, 10) + test.Substring(20);

// 从第10开始删除 10 个字符

var sliceUsingRemove = test.Remove(10, 10);

 

当使用于这种情况时, remove 明显更简单,更易读。

 

Replace() – 替换 char 或 String

将此实例中的指定 Unicode 字符或 String 的所有匹配项替换为其他指定的 Unicode 字符或 String

 

两种重载的形式

 

  • Replace(char oldChar, char newChar)

    将此实例中的指定 Unicode 字符的所有匹配项替换为其他指定的 Unicode 字符。

Replace(string oldValue, string newValue)

将此实例中的指定 String 的所有匹配项替换为其他指定的 String

 

 

 

string test = "Now is the time for all good men to come to the aid of their country.";


var politicallyCorrect = test.Replace("men", "people");


var spacesToPipes = test.Replace(' ', '|');



var withoutThe = test.Replace("the ", string.Empty);

下一个例子是当你有一块数据包含 “<BR/>” HTML 的代码时,想将它换成 Environment.NewLine:

 

string test = "Some data &amp; markup was loaded from a data source.<BR/>&nbsp;Oh look, we started a new line!";



var cleansedData = test.Replace("<BR/>", Environment.NewLine);



var moreCleansedData = test.Replace("&amp;", "&")

.Replace("&nbsp;", " ")

.Replace("<BR/>", Environment.NewLine);

同时我对stringbuilder进行了相同的实验,发现在这种情况下stringbuilder 的运行速度非常的缓慢:

 

var morePerformantCleansedData = new StringBuilder(test)

.Replace("&amp;", "&")

.Replace("&nbsp;", " ")

.Replace("<BR/>", Environment.NewLine)

.ToString();
更多精彩的相关文章,请阅读喜乐的ASP.NET(Alex Song)

参考翻译自 C#/.NET Little Wonders: The String Remove() and Replace() Methods

posted @ 2011-09-27 02:56  拥有的都是恩典  阅读(65839)  评论(6编辑  收藏  举报