Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting every character of a string? [duplicate]

Tags:

string

c#

split

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.

like image 895
Future King Avatar asked Jun 13 '10 20:06

Future King


People also ask

Can a string be split on multiple characters?

Method 1: Split multiple characters from string using re. split() This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.

How do you split a string at every nth character?

To split a string every nth character:Use a list comprehension to iterate over a range with step N. On each iteration, use string slicing to select a slice of the string. The list will contain string slices with maximum length of N.

How do you separate the characters in a string?

It's as simple as: s. split(""); The delimiter is an empty string, hence it will break up between each single character.


2 Answers

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.

like image 177
rossipedia Avatar answered Oct 04 '22 05:10

rossipedia


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.

like image 45
Sebastian P.R. Gingter Avatar answered Oct 04 '22 05:10

Sebastian P.R. Gingter