[英]How to determine if one string contains words from another string
我想檢查一個字符串是否包含C#中任意順序的單詞。 如何使用簡單的if語句執行此操作。 請參閱以下示例:
string a = "PT. DOWELL ANADRILL SCHLUMBERGER";
string b = "ANADRILL DOWELL";
if( a.Contains( b ) ) {
MessageBox.Show("true");
} else {
// the if always evaluates to false
MessageBox.Show("false");
}
如果您想知道a
是否在b
包含任何單詞,則
if(b.Split().Any(x => a.Contains(x)))
或者,如果您希望a
包含b
所有單詞
if(b.Split().All(x => a.Contains(x)))
注意既不確保在話b
不在子詞a
像
a = "Once before";
b = "be";
而且這只會在空白處拆分b
的單詞,因此如果存在您想忽略的標點符號(例如)
a = "I like turtles";
b = "like.";
這是另一種方法,如果您知道每個單詞之間要檢查一個空格。
if(b.Split(' ').Any(a.Contains))
string[] words = names.Split(';', '=', '\n', ' ', '\t');
String utga = textBox6.Text;
String utga1 = "IMSI";
int loop = 0;
int i = 0;
int j = 0;
foreach (string word in words)
{
int j = i+1;
if (words[i] == utga)
{
string loop = words[i+1];
if(words[j] == utga1)
{
MySqlDataAdapter adp1 = new MySqlDataAdapter("Insert into hlr.hlr(IMSI,MSISDN_COLUMN) Values('" + loop + "','" + words[i + 1] + "')", connection);
DataTable hlr = new DataTable();
adp1.Fill(hlr);
dataGridView1.DataSource = hlr;
adp1.Dispose();
connection.Close();
//i = i + too;
i++;
}
else
{
i++;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.