2017-12-12 7 views
1

私は文字列の配列を持っています。私はJSONから取得している文字列と比較したいと思います。比較はこのようにすべきです。 例: 一方の文字列のアカウント名がGoogleで、それ以外がGoogle Incの場合、GoogleはGoogle Incの会社名の一部であるため、一致するはずです。そうでなければ。文字列のC#

私が書いたコード:

for (int i = 0; i < accountCount; i++) 
     { 
      //// account is found in the array     
      name[i] = account.Entities[i].Attributes["name"].ToString(); 
      if (name[i] == message.Current.org_name) 
      { 
       flag = 1; 
       c.CreateOpportunity(message); 
       break; 
      } 
     }     
      //// account is not found in the array     
     if (flag == 0) 
     { 
      c.CreateAccount(message); 
      c.CreateOpportunity(message); 
     } 
+1

StringComparison Enumeration [ 'String.IndexOf'] (https ://msdn.microsoft.com/en-us/library/k8b1470s.aspx)と['String.Contains'](https://msdn.microsoft.com/en-us/library/dy85x1sa(v = vs。 110).aspx)はあなたの友人です。 – lamandy

+0

あなたは 'if(name [i] .Contains(mySearchString))'をしたいと思います。 – HimBromBeere

+0

@HimBromBeereそれは完璧です – vidhi

答えて

0

代わりContains機能を使用してみてください:

for (int i = 0; i < accountCount; i++) 
{ 
    //// account is found in the array     
    name[i] = account.Entities[i].Attributes["name"].ToString(); 
    if (name[i].Contains(message.Current.org_name) 
     || message.Current.org_name.Contains(name[i])) 
    { 
     flag = 1; 
     break; 
    } 
}     
//// account is not found in the array     
if (flag == 0) 
    c.CreateAccount(message); 
c.CreateOpportunity(message); 
+0

私はこれを試しました。たとえば、name [i] = Agniketおよびmessage.Current.org_name = Agniです。それはうまくいかなかった。 – vidhi

+0

これは不可能です。ステップごとにデバッグを確認してください。別の行エラーが必要です。 "Agniket" .Contains( "Agni") 'は常にtrueです。 – GGO

+0

実際これは次のようなものです:name [i] .Contains(message.Current.org_name) – vidhi

0

あなたはより多くのオプションをspeficifyするために大文字と小文字が区別検索に

またはIndexOfContainsを使用することができます比較基準付き

は、しかし、それの楽しみのために私たちは、配列またはリスト

ノートにAnyを使用することができます。ご希望の場合はnull

のための上記のチェックのどれも

var org = message.Current.org_name; 

var found = account.Entities.Any(// does any entity contain org ? 
    x => x.Attributes["name"]  // Get Attribute 
     .ToString()    // Convert it to string if needed 
     .Contains(org));   

if (found) 
{ 
    c.CreateAccount(message); 
} 

c.CreateOpportunity(message); 

を含みませんあなたが使うことができる大文字小文字の無感情な検索String.IndexOf

インデックス

var found = account.Entities.Any(// does any entity contain org ? 
    x => x.Attributes["name"]  // Get Attribute 
     .ToString()    // Convert it to string if needed 
     .IndexOf(org, StringComparison.OrdinalIgnoreCase) >= 0);  

参照

String.Contains - String.Contains Method (String)

String.IndexOf - String.IndexOf Method (String, StringComparison)

Coparison型 -