Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Eg. with LINQ:</p> <pre><code>string toCheck = "your string here"; bool isLong = toCheck .Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries) .Any(s =&gt; s.Length &gt; 50); </code></pre> <hr> <p><strong>Edit</strong></p> <p>Out of curiosity, with people suspecting Regex would be much faster (I assumed so too), I've run few simple tests. Have to admit I'm surprised with results:</p> <p><em>LINQ (or precisely, string.Split and LINQ) seems to perform over 3-20 times faster than compiled Regex and 6-30 than uncompiled one.</em></p> <p>I've run <code>1'000'000</code> iterations of each solution under <code>Release</code> mode, checking 4 sample strings:</p> <ul> <li>one with none 50-chars+ long words</li> <li>one with exactly one 50-chars+ long word, at the <strong>end</strong> of the string</li> <li>one with exactly one 50-chars+ long word, at the <strong>begining</strong> of the string</li> <li>one with multiple 50-chars+ long words, spread around the string</li> </ul> <p>Results can be seen here (LINQ vs compiled regex):</p> <blockquote> <p>LINQ [noLongWords], 1000000 iterations. Result = False: 867 ms</p> <p>LINQ [oneLongWordAtEnd], 1000000 iterations. Result = True: 986 ms</p> <p>LINQ [oneLongWordAtBegining], 1000000 iterations. Result = True: 827 ms</p> <p>LINQ [manyLongWordsEverywhere], 1000000 iterations. Result = True: 2399 ms</p> <p>Regex [noLongWords], 1000000 iterations. Result = False: 16714 ms</p> <p>Regex [oneLongWordAtEnd], 1000000 iterations. Result = True: 14225 ms</p> <p>Regex [oneLongWordAtBegining], 1000000 iterations. Result = True: 6483 ms</p> <p>Regex [manyLongWordsEverywhere], 1000000 iterations. Result = True: 6675 ms</p> </blockquote> <p>Source code for tests is available <a href="http://pastebin.com/DfMEC9C4" rel="nofollow">here</a>.</p> <p>Of course, under regular conditions (who sane runs <code>1'000'000</code> iterations?), difference is irrelevant and one should go for easier/more maintainable solution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload