如何在C#中将单词的字母拆分为数组? [英] How do I split a word's letters into an Array in C#?
本文介绍了如何在C#中将单词的字母拆分为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#中将字符串拆分为字符数组?
How do I split a string into an array of characters in C#?
示例字符串单词是"robot".
Example String word used is "robot".
程序应打印出:
r
o
b
o
t
原始代码段:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace Testing
{
class Program
{
static void Main(string[] args)
{
String word = "robot";
String[] token = word.Split(); // Something should be placed into the () to
//print letter by letter??
foreach (String r in token)
{
Console.WriteLine(r);
}
}
}
}
如何正确执行代码?
推荐答案
为什么不呢?
public void WriteLetters(string s)
{
foreach (char c in s)
{
Console.WriteLine(c);
}
}
如果您真的想在显式字符数组中查看它,请执行以下操作:
If you really want to see it in an explicit character array, do the following:
string s = "robot";
char[] charArray = s.ToCharArray();
如果随后要打印出来(每行1个),请执行以下操作:
If you then want to print that out (1 per line), do the following:
for (int i = 0; i < charArray.Length; i++)
{
Console.WriteLine(charArray[i]);
}
我不太确定您要做什么,所以我可能不太了解.
I'm not really sure what you're trying to do, so I may be way off base.
如果通过标记化,是指将字符存储在数组中,则已经将其存储在String对象中. 1 .
If by tokenize, you mean store the characters in an array, you already have that in a String object1.
1 并非如此,因为String使用索引器来遍历字符值,而在C/C ++中,字符串实际上是一个数组人物.出于我们的目的,我们可以将字符串视为字符数组
这篇关于如何在C#中将单词的字母拆分为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文