【问题标题】:Separate words in the textbox C#在文本框中分隔单词 C#
【发布时间】:2020-11-20 16:10:15
【问题描述】:

我想在文本框中写一个这样的文本:“Sentence1::Sentence2::Sentence3”然后把它分成三个部分,分别放在三个单独的字符串中。我使用了 string.split 方法,但我无法将它们放入字符串中。

string authors = "Sentence1::Sentence2::Sentence3";
string[] authorsList = authors.Split("::");

【问题讨论】:

  • “不能将它们放入字符串”是什么意思?
  • 有什么理由不能只使用您创建的字符串数组?为什么需要将它们放入其他字符串变量中?您可以通过 Idle_Mind 向您展示的索引来访问它们。
  • 你有 3 个字符串:authorList[0]、authorList[1] 和 authorList[2]

标签: c# string text split textbox


【解决方案1】:

Split() 之后有一个字符串数组。您可以通过索引访问字符串。这是一个例子:

string authors = "Sentence1::Sentence2::Sentence3";
string[] authorsList = authors.Split("::");

for(int i=0; i<authorsList.Length; i++) {
  Console.WriteLine(i + ": " + authorsList[i]);
}

输出:

0: Sentence1
1: Sentence2
2: Sentence3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-22
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多