Splitting every character of a string? [duplicate]
I want to split a string into each single character. Eg:
Splitting : "Geeta" to "G", "e", "e" , "t", "a"
How can I do this? I want to split a string which don't have any separator
Please help.
String.ToCharArray()
From MSDN:
This method copies each character (that is, each Char object) in a string to a character array. The first character copied is at index zero of the returned character array; the last character copied is at index Array.Length – 1.
you can use a simple for-loop with chars:
foreach (char ch in stringVar)
{
Console.WriteLine(ch.ToString());
}
I fact you don't need to split it, because you already can acces every single char element in a string of its own.
You can iterate over the string like this:
foreach (char c in myString)
{
Console.WriteLine(c);
}
You can do the following. Though it is not an effective way to do.
public class Main {
public static void main(String[] args) {
String str ="Noman";
for(int i = 0; i<str.length(); i++){
System.out.println(str.charAt(i) +" ");
}
}
}
精彩评论