我想使用c#检查字符串值是否包含字符串数组中的单词。例如,
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext", etc... };
if(stringToCheck.contains stringArray) //one of the items?
{
}
我如何检查字符串值'stringToCheck'是否包含数组中的一个词?
我想使用c#检查字符串值是否包含字符串数组中的单词。例如,
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext", etc... };
if(stringToCheck.contains stringArray) //one of the items?
{
}
我如何检查字符串值'stringToCheck'是否包含数组中的一个词?
当前回答
演示了三个选项。我认为第三条是最简洁的。
class Program {
static void Main(string[] args) {
string req = "PUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.B"); // IS TRUE
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.C"); // IS TRUE
}
req = "UT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("two.1.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("three.1.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.A"); // IS TRUE
}
req = "XPUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.B"); // false
}
req = "PUTX";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.C"); // false
}
req = "UT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.D"); // false
}
req = "PU";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.E"); // false
}
req = "POST";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("two.2.A"); // IS TRUE
}
req = "ASD";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("three.2.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.B"); // false
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.C"); // false
}
req = "UT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("two.3.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("three.3.A"); // false
}
Console.ReadKey();
}
}
其他回答
使用LINQ和方法组将是最快和更紧凑的方法。
var arrayA = new[] {"element1", "element2"};
var arrayB = new[] {"element2", "element3"};
if (arrayB.Any(arrayA.Contains))
return true;
Try:
String[] val = { "helloword1", "orange", "grape", "pear" };
String sep = "";
string stringToCheck = "word1";
bool match = String.Join(sep,val).Contains(stringToCheck);
bool anothermatch = val.Any(s => s.Contains(stringToCheck));
string [] lines = {"text1", "text2", "etc"};
bool bFound = lines.Any(x => x == "Your string to be searched");
如果搜索的字符串与数组'lines'中的任何元素匹配,则bFound设置为true。
⚠️ Note: this does not answer the question asked |
---|
The question asked is "how can I check if a sentence contains any word from a list of words?" |
This answer checks if a list of words contains one particular word |
只需使用LINQ方法:
stringArray.Contains(stringToCheck)
演示了三个选项。我认为第三条是最简洁的。
class Program {
static void Main(string[] args) {
string req = "PUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.B"); // IS TRUE
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.C"); // IS TRUE
}
req = "UT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("two.1.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("three.1.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.A"); // IS TRUE
}
req = "XPUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.B"); // false
}
req = "PUTX";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.C"); // false
}
req = "UT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.D"); // false
}
req = "PU";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.E"); // false
}
req = "POST";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("two.2.A"); // IS TRUE
}
req = "ASD";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("three.2.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.B"); // false
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.C"); // false
}
req = "UT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("two.3.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("three.3.A"); // false
}
Console.ReadKey();
}
}