簡體   English   中英

如何確定一個字符串是否包含來自另一個字符串的單詞

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM