Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a single string into an array of strings?

I have a program in which I read from a string that's formatted to have a specific look. I need the numbers which are separated by a comma (e.g. "A,B,D,R0,34,CDF"->"A","B","D","R0", "34", "CDF"). There are commas between letters that much is guaranteed I have tried to do (note: "variables" is a 2D char array, newInput is a string which is a method argumend, k and j are declared and defined as 0)

for(int i=0; i<=newInput.Length; i++){
            while(Char.IsLetter(newInput[i])){
                variables[k,j]=(char)newInput[i];
                i++;
                k++;
            }
            k=0;j++;

        }

with multi-dimensional character arrays. Is there a way for this to be done with strings? Because char arrays conflict with many parts of the program where this method has already been implemented

like image 367
Mislav Javor Avatar asked Feb 16 '14 05:02

Mislav Javor


People also ask

Which function breaks a string into an array?

Using split() If the string and separator are both empty strings, an empty array is returned. The following example defines a function that splits a string into an array of strings using separator .

How do you break a string into multiple parts?

As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.

What is the string method to break a string into pieces?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.


1 Answers

Simple. Just use the Split method:

var input = "A,B,D,R0,34,CDF";
var output = input.Split(','); // [ "A", "B", "D", "R0", "34", "CDF" ]
like image 62
p.s.w.g Avatar answered Oct 19 '22 23:10

p.s.w.g