新卒から文系エンジニア→人材業界に転職した人のブログ

新卒から文系エンジニア→人材業界に転職。技術・スキルがないためブログを通して勉強。その後、IT業界の業界知識が活かせる人材業界へ。異業種×異職種の転職経験有り。

このエントリーをはてなブックマークに追加

【ASP.NET・C#】カンマ区切りのデータを処理するときの話 -splitではなく、string.Contains-

【内容】

カンマ区切りのデータを処理するときの方法についてのメモ。


■前提

前提として、
・「,」区切りを文字列で取得後、ある文字列があるかを判定したいとき
 例 検索対象code1,code2,code3
   検索文字列code3


■コーディング方法

1.「,」区切りをいちいちsplitして配列に詰めなおした後、検索
2.「,」区切りのまま検索(new)


単純に検索文字列の存在チェックであるならば、2のようにsplitする必要はない
参考ソースをみてばかりいて、ほかによりよい他のコーディング方法がないか調べていなかったこと
気づかなかった原因?
参考にしていたソースがわるかったことも原因?


参考:
String.Contains メソッド (System)

■参考ソース


public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = "code1,code2,code3";
        string SerchVal = "code3";
        bool bl1 = str.Contains(SerchVal);
        Debug.WriteLine(bl1);

       string []strA = str.Split(',');
       bool bl2 = strA.Contains(SerchVal);
       Debug.WriteLine(bl2);
    }
}
.hatena-module:nth-of-type(10) { background: transparent; } .hatena-module:nth-of-type(10) .hatena-module-title{ display: none; } .hatena-module:nth-of-type(10) .hatena-module-body { padding: 0; }