繁体   English   中英

我如何解析字符串中的特定字符/字母并将其添加到列表中 <string> ?

[英]How can i parse specific char/letter from string and add it to a List<string>?

txtDir是一个textBox,如果我只在hello中键入内容,那么在test1中应该只是一次单词hello。但是,如果我在textBox中键入hello1 / hello2 / hello3,则在test1中我应该看到

hello1 hello2 hello3

List<string> test1 = new List<string>();
int index = 0;
while(true)
{
    index = txtDir.Text.IndexOf("/");
    string text = txtDir.Text.Substring(0, index);
    test1.Add(text);
    newNodeParsed = new TreeNode(text);
    rootNode.Nodes.Add(newNodeParsed);
}

我如何解析并获取每个'/'之间的文本并将其添加到列表中?

将输入字符串拆分为字符串字段的字符串数组,并将该数组转换为列表:

List<string> test1 = txtDir.Text.Split('/').ToList();

假设您要在test1上为textBox中的每个单词插入值(用'/'字符分隔),您应该这样做:

List<String> test1 = new List<String>();
test1.AddRange(txtDir.Text.Split('/'));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM