tags:

views:

103

answers:

4
+2  Q: 

C# Linq non-vowels

From the given string

(i.e)

string str = "dry sky one two try";
var nonVowels = str.Split(' ').Where(x => !x.Contains("aeiou")); (not working).

How can i extract non-vowel words?

+2  A: 

Something like:

var nonVowelWords = str.Split(' ').Where(x => Regex.Match(x, @"[aeiou]") == null);
Paul Betts
+3  A: 

This should work:

var nonVowels = str.Split(' ').Where(x => x.Intersect("aeiou").Count() == 0);

String.Contains requires you to pass a single char. Using Enumerable.Contains would only work for a single char, as well - so you'd need multiple calls. Intersect should handle this case.

Reed Copsey
Thanks you very much.Suppose if i were to use C# 2.0 how can i handle it?
The same way, but call the extension methods as static methods and use an anonymous delegate in stead of the lambda: `delegate(string x) { return Enumerable.Count(Enumerable.Intersect(x, "aeiou")) == 0; }`
Joren
This is *terrible* performance compared to my answer.
280Z28
@ Joren Thank you.
+1  A: 
string str = "dry sky one two try";
var nonVowels = str.ToCharArray()
    .Where(x => !new [] {'a', 'e', 'i', 'o', 'u'}.Contains(x));
Darin Dimitrov
Should be `str.Split`, not `ToCharArray`.
Kobi
+4  A: 

Come on now y'all. IndexOfAny is where it's at. :)

// if this is public, it's vulnerable to people setting individual elements.
private static readonly char[] Vowels = "aeiou".ToCharArray();

// C# 3
var nonVowelWorks = str.Split(' ').Where(word => word.IndexOfAny(Vowels) < 0);

// C# 2
List<string> words = new List<string>(str.Split(' '));
words.RemoveAll(delegate(string word) { return word.IndexOfAny(Vowels) >= 0; });
280Z28
Opps!!!!1 Excellent.