2014-10-27 168 views
-1

我正在努力正確映射網站上的鏈接。計算字符串模式的出現次數,使用Linq的字符串

我需要能夠計算../在字符串中出現的頻率。 在這一刻我有一個功能,循環的字符串和計數,而這個工程,即時通訊尋找Linq解決方案

我知道,我可以這樣

int count = Href.Count(f => f == '/'); 

單個字符計數,但,我可以通過使用LINQ,算上模式../出現的頻率?這可能嗎?

+3

爲什麼你要使用LINQ,這似乎是一個正則表達式的工作? – 2014-10-27 09:46:14

+3

我有一把錘子。我需要把這個螺絲釘插入牆上。我怎麼用錘子打它? – 2014-10-27 09:46:15

+0

幸運的是,如果直接在字符串上使用LINQ,那麼在IEnumerable 上使用LINQ時,不會使用LINQ,因爲您可以真正離開LINQ是一個索引枚舉。 我會看看http://stackoverflow.com/questions/541954/how-would-you-count-occurrences-of-a-string-within-a-string – tolanj 2014-10-27 09:49:20

回答

1

您可以使用此擴展方法:

public static int ContainsCount(this string input, string subString, bool countIntersecting = true, StringComparison comparison = StringComparison.CurrentCulture) 
{ 
    int occurences = 0; 
    int step = countIntersecting ? 1 : subString.Length; 
    int index = -step; 
    while ((index = input.IndexOf(subString, index + step, comparison)) >= 0) 
     occurences++; 
    return occurences; 
} 

返回給定的字符串用純字符串的方法在子串的數量:

int count = Href.ContainsCount("../"); 

String-methods在效率方面優於其他使用LINQ或正則表達式的方法。

此方法支持計數相交的子字符串(默認)和非重疊的子字符串。

這顯示了差異:

string str = "ottotto"; 
int count = str.ContainsCount("otto");  // 2 
count = str.ContainsCount("otto", false); // 1 
1

是的,這是可能的,但它很尷尬,它會很慢,而且很難閱讀。不要使用它。

How would you count occurrences of a string within a string?

src.Select((c, i) => src.Substring(i)).Count(sub => sub.StartsWith(target)) 

或者,這看起來很漂亮:

public static class StringExtensions 
{ 
    public static IEnumerable<int> IndexOfAll(this string input, string value){ 
     var currentIndex = 0; 

     while((currentIndex = input.IndexOf(value, currentIndex)) != -1) 
      yield return currentIndex++; 
    } 
} 

與用法:

"TESTHATEST" 
    .IndexOfAll("TEST") 
    .Count() 
    .Dump(); 
+0

當然'Href.Count(x => x.Contains(「../」));'更容易閱讀? .. – 2014-10-27 09:47:27

+1

@SimonWhitehead;你憑什麼呢?上面的LINQ似乎有缺陷,因爲 .Contains(「string」)沒有意義。 – 2014-10-27 09:49:05

+0

公平點。睡覺對我來說! – 2014-10-27 09:53:03

2

你可以做到這一點很好地與正則表達式

var dotdotslash=new Regex(@"\.\./"); 
string test="../../bla/../"; 
int count=dotdotslash.Matches(test).Count; 

3 
0

正則表達式(見梅德Ledentsov的答案)是這裏要好得多;然而LINQ的也是可能的:

String source = @"abc../def../"; 

    // 2 
    int result = source 
    .Where((item, index) => source.Substring(index).StartsWith(@"../")) 
    .Count(); 
0

其實,你可以做一個真正LINQy(和尷尬:))的方式是這樣的:

private static int CountPatternAppearancesInString(string str, string pattern) 
{ 
    var count = str 
     .Select(
      (_, index) => 
       index < str.Length - pattern.Length + 1 && 
       str.Skip(index) 
        .Take(pattern.Length) 
        .Zip(pattern, (strChar, patternChar) => strChar == patternChar) 
        .All(areEqual => areEqual)) 
     .Count(isMatch => isMatch); 

    return count; 
} 

或者,使用一些字符串提供的方法:

private static int CountPatternAppearancesInString(string str, string pattern) 
{ 
    var count = str 
     .Select(
      (_, index) => 
       index < str.Length - pattern.Length + 1 && 
       str.IndexOf(pattern, index, pattern.Length) >= 0) 
     .Count(isMatch => isMatch); 

    return count; 
} 

但是,正如已經說過的那樣,它不是最理想的,僅用於說明的目的。

相關問題