如何检查字符串是否包含单词的所有字符 [英] How to check if a string contains all of the characters of a word
问题描述
我想检查一个字符串是否包含给定单词的所有字符,例如:
I wish to check if a string contains a all of the characters of a word given, for example:
var inputString = "this is just a simple text string";
说我有这个词:
var word = "ts";
现在应该选择包含 t 和 s 的单词:
Now it should pick out the words that contains t and s:
这只是字符串
这就是我正在从事的工作:
This is what I am working on:
var names = Regex.Matches(inputString, @"\S+ts\S+",RegexOptions.IgnoreCase);
但是,这并没有给我回我喜欢的词.如果我只喜欢 t 这样的字符,它将把所有包含 t 的单词还给我.如果我使用的是 st 而不是 ts ,它将带给我单词 just .
however this does not give me back the words I like. If I had like just a character like t, it would give me back all of the words that contains t. If I had st instead of ts, it would give me back the word just.
关于它如何工作的任何想法?
Any idea of how this can work ?
推荐答案
这是LINQ解决方案,与正则表达式相比,它在眼睛上更自然.
Here is a LINQ solution which is easy on the eyes more natural than regex.
var testString = "this is just a simple text string";
string[] words = testString.Split(' ');
var result = words.Where(w => "ts".All(w.Contains));
结果是:
此
只是
字符串
this
just
string
这篇关于如何检查字符串是否包含单词的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!