问题
How do I split a string into an array of characters in C#?
Example String word used is "robot".
The program should print out:
r
o
b
o
t
The orginal code snippet:
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);
}
}
}
}
How can the codes be correctly implemented?
回答1:
Why not this?
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();
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.
If by tokenize, you mean store the characters in an array, you already have that in a String object1.
1 Not actually true, because String uses an indexer to iterate through the character values, whereas in C/C++, a string is actually an array of characters. For our purposes, we can treat a string as if it is an array of characters
回答2:
The class String implements IEnumerable<char> and therefore to run through the letters of a string, you can just grab an IEnumerator<char> and process the char
s one at a time. One way to do this is using a foreach:
foreach(char c in "robot") {
Console.WriteLine(c);
}
If you need each char
in an array you can just use String.ToCharArray:
char[] letters = "robot".ToCharArray();
回答3:
Do you mean something like this?
foreach(var alpha in myString.Where(c => Char.IsLetter(c)))
{
Console.WriteLine(alpha);
}
回答4:
You can using ToArray() method to return the char array.
string word = "robot";
char[] array = word.ToArray();
回答5:
You are trying to do too much.
Try this:
String word = "robot";
foreach (char letter in word)
{
Console.WriteLine(letter);
}
Edit:
To split the string into character array, without a loop, you can do this: word.ToCharArray()
回答6:
Seems everyone wants to convert a String into an Array of Chars.
How about
for(int i = 0; i < tmpString.Length; i++)
Console.WriteLine(tmpString[i]);
Now you have the speed of a char array without the extra memory of making a copy.
edit: A String is an array of chars internally, there just isn't a way to change their values because String are immutable. But you can read from that char array. String = Read-Only char array.
I can't think of any reason to convert a String into a Char[] unless you wanted to "edit" the string.
long lTicks;
char[] tmpChar = { 'a', 'b', 'c', 'd', 'e' };
String tmpString = "abcde";
char chRead;
lTicks = DateTime.Now.Ticks;
for (int i = 0; i < 100000000; i++)
chRead = tmpChar[i%5];
Console.WriteLine(((DateTime.Now.Ticks - lTicks) / 10000).ToString());
lTicks = DateTime.Now.Ticks;
for (int i = 0; i < 100000000; i++)
chRead = tmpChar[i % 5];
Console.WriteLine(((DateTime.Now.Ticks - lTicks) / 10000).ToString());
lTicks = DateTime.Now.Ticks;
for (int i = 0; i < 100000000; i++)
chRead = tmpString[i%5];
Console.WriteLine(((DateTime.Now.Ticks - lTicks) / 10000).ToString());
lTicks = DateTime.Now.Ticks;
for (int i = 0; i < 100000000; i++)
chRead = tmpString[i % 5];
Console.WriteLine(((DateTime.Now.Ticks - lTicks) / 10000).ToString());
Console.ReadLine();
Kind of funny, the String is actually consistently faster than the Char[]. I ran each twice to make sure there wasn't a load time issue affecting the results. Compiled as Release with optimizations. Char[] was ~1950ms and String ~1850ms every run for me.
来源:https://stackoverflow.com/questions/4246856/how-do-i-split-a-words-letters-into-an-array-in-c