2016-04-05 2 views
0

"test"という文字列を分割し、Nameの値をtextboxに、okの値をCheckboxに割り当てるコードを記述します。 OKの値がYESであると、チェックボックスをチェックする必要があります"test"という文字列を分割し、Nameの値をtextboxに代入し、okの値をCheckboxに代入するコードを記述します。

た.aspxページで

名:aspx.csページで(チェックボックス)

:(テキストボックス)

[OK]を、

 protected void Page_Load (object sender, EventArgs e) 

     { 

      String test = “Name = ADP India Pvt LTD;OK = YES “; 

      } 
+0

問題は何ですか? – S4beR

答えて

1

これは単純に二つの部分にあなたの既存の文字列を破るためにString.Split()方法を使用して、その後、各セクションの値をチェックの問題です:

// Example string 
string test = "Name = ADP India Pvt LTD;OK = YES "; 
// Split this into two sections (using the ';' as a delimiter) 
var sections = test.Split(';'); 
// Now the first entry will be the name, so we need the section after 
// the equals sign 
Name.Text = sections[0].Split('=')[1].Trim(); 
// Based on the value of your "OK", determine if your checkbox should be checked 
Ok.Checked = (sections[1].Split('=')[1].Trim()== "YES"); 

これは、最も安全な例ではありませんが、このような問題の解決方法について考えてください。

あなたはLINQを使用し気にしない場合は、辞書にあなたの文字列をマッピングすることにより、もう少し簡単にこの問題を解決することができます:

// Example string 
string test = "Name = ADP India Pvt LTD;OK = YES"; 
// Map each key (e.g. "Name") and value (e.g. "ADP Index Pvt LTD") 
// to an entry in a dictionary 
var dictionary = test.Split(';') 
        .ToDictionary(k => k.Split('=')[0].Trim(), 
            v => v.Split('=')[1].Trim()); 

// Now reference what you need by it's key 
Name.Text = dictionary["Name"];   // yields "ADP India Pvt LTD"; 
Ok.Checked = dictionary["OK"] == "YES"; // checks the checkbox if "YES" 
+0

ありがとうございました。 – Thennarasu

関連する問題