2011-08-10 13 views
0

申し訳ありません..私はしかし、今回は私が特定の文字で終わるすべての単語を検索したい...非常によく似た質問以前に求めている特定の文字で始まり、終わるすべての単語を返す方法は?

 List<string> words = new List<string>(); 
     words.Add("abet"); 
     words.Add("abbots"); //<---Return this 
     words.Add("abrupt"); 
     words.Add("abduct"); 
     words.Add("abnats"); //<--return this. 
     words.Add("acmatic"); 


     //Now return all words of 6 letters that begin with letter "a" and has "ts" as the 5th and 6th letter 
     //the result should return the words "abbots" and "abnats" 
     var result = from w in words 
        where w.Length == 6 && w.StartsWith("a") && //???? 

答えて

2

Iの避難所を次のように私は、単語のリストを持っていますこれをコンパイルしてテストしましたが、うまくいくはずです。

var result = from w in words 
        where w.Length == 6 && w.StartsWith("a") && w.EndsWith("ts") 
1

最後に文字を確認するにはEndsWithを使用してください。

var result = from w in words 
        where w.Length == 6 && w.StartsWith("a") && w.EndsWith("ts") 

使用IndexOf特定の位置(あなたのケースで5番目から始まる)で始まる単語を確認する:

var result = from w in words 
        where w.Length == 6 && w.StartsWith("a") && (w.Length > 5 && w.IndexOf("ts", 4)) 
0

ちょうどサフィックスのため.EndsWith()を使用します。

var results = from w in words where w.Length == 6 
    && w.StartsWith("a") 
    && w.EndsWith("ts"); 
0

あなたはEndsWith()機能を使用することができる:

用途:

var test= FROM w in words 
      WHERE w.Length == 6 
       && w.StartsWith("a") 
       && w.EndsWith("ts"); 

代替:

var test = words.Where(w =>w.Length==6 && w.StartsWith("a") && w.EndsWith("ts")); 
0

Aの正規表現は、ここではあなたの友達です:

Regex regEx = new Regex("^a[A-Za-z]*ts$"); 
var results = from w in words where regEx.Match(w).Success select w; 

また、LINQのクエリ理解構文を使用した場合よりも注意してください、あなたは(それだけで元from変数の場合も同様です。)それの終わりにselectが必要になります

0

あなたは正規表現のビットを試すことができますあなたがそれまで感じている場合:

これは、「a」で始まり「a」で終わるものと一致する必要があります。

関連する問題