如何检查字符串是否包含单词的所有字符 [英] How to check if a string contains all of the characters of a word

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

问题描述

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆