首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编写一段代码来拆分字符串“test”,并将Name的值赋给textbox,将ok的值赋给Checkbox。

编写一段代码来拆分字符串“test”,并将Name的值赋给textbox,将ok的值赋给Checkbox。
EN

Stack Overflow用户
提问于 2016-04-05 22:12:18
回答 1查看 134关注 0票数 0

编写一段代码来拆分字符串“test”,并将Name的值赋给textbox,将ok的值赋给Checkbox。由于OK的值为YES,因此应选中该复选框

在.aspx页面中,

名称:(文本框)

Ok:(复选框)

在aspx.cs页面中,

代码语言:javascript
运行
复制
       protected  void Page_Load (object sender, EventArgs e)

       {

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

          }
EN

回答 1

Stack Overflow用户

发布于 2016-04-05 22:20:31

这只需要使用String.Split()方法将现有字符串分成两个部分,然后检查每个部分的值:

代码语言:javascript
运行
复制
// 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,你可以通过将字符串映射到字典来更容易地解决这个问题:

代码语言:javascript
运行
复制
// 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"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36428866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档