Results 1 to 7 of 7
Thread: Question about split()
- 03-23-2012, 05:36 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Question about split()
How come when I pass a String that looks like this:
(6 + 2) + 2
When I use the split method the first index in tokens has nothing, and then the second index has "("? The first index should have "(" not nothing. Isn't that was trim() does?
Java Code:String[] tokens = infix.trim().split("");
-
Re: Question about split()
String.split allows you to specify a delimiter to split the String by.
Try something like this instead:
Java Code:String s = "split-this-string"; String[] sArr = s.split("-");
You can find out more about String methods by looking at the String java doc:
String (Java 2 Platform SE v1.4.2)
- 03-23-2012, 06:01 AM #3
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: Question about split()
If I have a string that is like this:
Java Code:String str = "(6 + 2) + 2";
{ "(", "6", "+", "2", ")", "+", "2" };
Notice spaces have been taken out as well.
-
Re: Question about split()
trim() wont help you here
Use String.replace(" ", "") to replace white space with nothing
Then use String.toCharArray() to return a char[] array which will contain each char in a separate array element of course.
(This is because in Java, String is really a wrapper for a char[] array)
As I said before, read the java docs to see what you can do.
Click the link I left above, scroll down to the Methods section. It tells you a description of each method so you'll know next time how you can do certain tasks.
- 03-23-2012, 06:30 AM #5
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: Question about split()
It doesn't makes sense that when I do this...
Java Code:String tokens = infix.trim(); tokens = infix.replace(" ", ""); String [] token = tokens.split("");
{ " ", "(", "6", "+", "2", ")", "+", "2" };
Or at least it is nothing in the first index...
- 03-23-2012, 07:26 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: Question about split()
It's like splitting an empty string, so as the result is the original string, an empty string. That is if you have a code like:
Java Code:String[] split = "".split("");
Website: Learn Java by Examples
- 03-23-2012, 08:00 AM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
Re: Question about split()
You should read whats posted by ozzyman
infix.replace(" ", "") --> your string is "(6+2)+2".
If you would use toCharArray, you would get an array of characters { '(', '6', '+', '2', ')', '+', '2' } - is that not enough? :D
If you really want to use split and want to have some strings like "6","+" ... try this:
String[] token = tokens.replace(" ", "").split("(?<=.)");
Similar Threads
-
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 05:08 PM -
split method question
By Chasingxsuns in forum New To JavaReplies: 3Last Post: 11-19-2009, 08:19 PM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 09:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:32 PM
Bookmarks