如何检查字符串是否包含单词的所有字符

如何检查字符串是否包含单词的所有字符

本文介绍了如何检查字符串是否包含单词的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个字符串是否包含给定单词的所有字符,例如:

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";

现在应该选择包含和的单词:

Now it should pick out the words that contains and :

这就是我正在从事的工作:

This is what I am working on:

var names = Regex.Matches(inputString, @"\S+ts\S+",RegexOptions.IgnoreCase);

但是,这并没有给我回我喜欢的词.如果我只喜欢这样的字符,它将把所有包含的单词还给我.如果我使用的是而不是,它将带给我单词.

however this does not give me back the words I like. If I had like just a character like , it would give me back all of the words that contains . If I had instead of , it would give me back the word .

关于它如何工作的任何想法?

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));

结果是:

这篇关于如何检查字符串是否包含单词的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:45