|
-
Dec 6th, 2005, 02:52 PM
#1
Thread Starter
Fanatic Member
Splitting a string that has no delimeter (or regex)
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 like
Last edited by x-ice; Feb 25th, 2007 at 07:07 PM.
-
Dec 6th, 2005, 07:04 PM
#2
Re: Splitting a string that has no delimeter (or regex)
Can't you just iterate through the string and use each character individually?
If not, try passing the empty string to split().
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 7th, 2005, 06:03 AM
#3
Thread Starter
Fanatic Member
Re: Splitting a string that has no delimeter (or regex)
Can't you just iterate through the string and use each character individually?
How would i do this?
-
Dec 7th, 2005, 06:50 AM
#4
New Member
Re: Splitting a string that has no delimeter (or regex)
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
-
Dec 7th, 2005, 08:01 AM
#5
Re: Splitting a string that has no delimeter (or regex)
 Originally Posted by x-ice
How would i do this?
Use toCharArray() to covert it to a character array...
Code:
char[] chars = "football".toCharArray();
for (int x=0; x<chars.length; x++)
System.out.println(chars[x]);
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Dec 7th, 2005, 11:21 AM
#6
Re: Splitting a string that has no delimeter (or regex)
Meh. In Java 5, String implements CharSequence and thus is iterable.
Code:
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));
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 7th, 2005, 01:31 PM
#7
Re: Splitting a string that has no delimeter (or regex)
 Originally Posted by CornedBee
Meh. In Java 5, String implements CharSequence and thus is iterable.
Code:
for(char c : str) {
System.out.println(c);
}
Witchcraft!
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|