开发者

Linq or string filter

I need a filter on the string which takes anothe开发者_运维知识库r string as a parameter, scans first string and removes all appearances of it.


You can use string.Replace which has an overload specifically for this.

var newString = oldString.Replace("foo", string.Empty);

This takes your oldString, finds all occurrences of "foo" and removes them.


This would work

var s = "string";
s = s.Replace("st", string.Empty);
// s == "ring";

Is that not correct?


Use extension methods:

public static class StringExtensions
{

    public static string RemoveOccurences(this string s, string occurence)
    {

         return s.Replace(occurence, "");    
    }

}

usage:

string s = "Remove all appearances of this and that and those";
s.RemoveOccurences("th");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜