开发者

Splitting every character of a string? [duplicate]

This question already has answers here: 开发者_开发百科 Split string into string array of single characters (8 answers) Closed 3 years ago.

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) +" ");
}
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜