Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting Strings into Characters??

Status
Not open for further replies.

sjohri214

Programmer
Jul 19, 2002
24
0
0
GB
Hi,

I was hoping that someone could tell me how to split a String into its constituent characters?
For example:

String seq = "ABCDEFGHIJ";

How can this String be split into characters
A
B
C
D
E
etc, and then assign those characters to separate strings?

e.g. String s1 = "A";
s2 = "B";
s3 = "C";
s4 = "D"; etc ?

Thanks for the help

 
Code:
for (i = 0; i < seq.length(); i++) {
   s[i] = seq.charAt(i);
}

That will create an array of strings. Just be sure to initialize the array before you run the loop.

-kaht
 
Thanks for the reply kaht,

I used the code u provided.. although it was showing an error message &quot;incompatible types&quot;.

So.. i used the following instead:
Code:
s = new String[sequence.length()];                         
            int j;
            for ( j = 0; j < sequence.length(); j++){                 
                s[j] = sequence.substring(j, j+1);
                System.out.println(s[j]);               
            }
 
odd, .charAt() is a valid string method......

-kaht
 
kaht,
Code:
&quot;s[i]=seq.charAt(i)&quot;
returns a char, so &quot;s[ i ]&quot; must be a char and not a String. So your code is correct if
Code:
&quot;s= new char[seq.length()]&quot;
So, depends on what sjohri214 wants, char or String.
 
duh.....

I've been doing too much javascript lately......
it lets you dynamically turn your variables into anything

-kaht
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top