Click to See Complete Forum and Search --> : Splitting a string that has no delimeter (or regex)
x-ice
Dec 6th, 2005, 01:52 PM
If i have a string "football" or "racing". How can i split this string? The String.split() method needs a delimeter (or as the documentation calls it a 'regex'). Because there is no regex i cannot use this method. Are there any other ways?
I want the new string to look likef
o
o
t
b
a
l
l
CornedBee
Dec 6th, 2005, 06:04 PM
Can't you just iterate through the string and use each character individually?
If not, try passing the empty string to split().
x-ice
Dec 7th, 2005, 05:03 AM
Can't you just iterate through the string and use each character individually?How would i do this?
~Rob
Dec 7th, 2005, 05:50 AM
int i = 0;
while (String.length<i);
{
System.out.println(i);
i++;
}
ok, so that's dirty code that won't work but it should give you an idea of the structure. You may need to input the text into an array list
crptcblade
Dec 7th, 2005, 07:01 AM
How would i do this?
Use toCharArray() to covert it to a character array...
char[] chars = "football".toCharArray();
for (int x=0; x<chars.length; x++)
System.out.println(chars[x]);
CornedBee
Dec 7th, 2005, 10:21 AM
Meh. In Java 5, String implements CharSequence and thus is iterable.
for(char c : str) {
System.out.println(c);
}
In 1.4, it's not necessary to convert to a char array, but the code above is quite broken.
[code]int len = str.length();
for(int i = 0; i < len; ++i) {
System.out.println(str.charAt(i));
}
crptcblade
Dec 7th, 2005, 12:31 PM
Meh. In Java 5, String implements CharSequence and thus is iterable.
for(char c : str) {
System.out.println(c);
}
Witchcraft!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.