【问题标题】:Split word into letters C# [duplicate]将单词拆分为字母C# [重复]
【发布时间】:2022-08-22 00:59:35
【问题描述】:

我试图将一个单词分成一个统一的所有字母的数组,例如input: hellooutput: h e l l o 有人帮助我吗? 谢谢。

  • \"hello\".ToArray()
  • 在内部,string 是char 的集合,所以任何集合拆分方法都会得到你想要的。 str.ToList() 或 str..Select(x => x) 等
  • 这回答了你的问题了吗? C# - How to convert string to char?

标签: c#


【解决方案1】:

您可以使用.ToCharArray() 将String 转换为char 的数组。

string input = "hello"; // your input string
string[] output = input.ToCharArray(); // ['h', 'e', 'l', 'l', 'o']

string glue = string.Join(" ", output); // glue the char array together with a space in-between
Console.WriteLine("The output is: " + glue); // shows the result (h e l l o)

glue = string.Concat(output); // glue together without a string/char in-between
Console.WriteLine("The output is: " + glue); // shows the result (hello)

我还在第 3 行添加了使用string.Join 将其粘合在一起的示例。

【讨论】:

    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 2011-05-13
    • 2016-01-16
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多