Hello all, i have a question. as the title says, how can i split a string and knowing the the contents of the string are like "2.98+4.17", add them? Thank you very much
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Koren Washington
Greenhorn
Joined: Mar 24, 2010
Posts: 3
posted
0
so lets say my string was s, how would i split it at the plus sign?
Look at the link I provided again because it is clearly explained what the regex is for. There is even a link to the pattern javadoc. And you probably need the method with only 1 argument.
Koren Washington wrote:yes i did, i put s.split(s, '+');
Unlikely - that would have produced a compiler error. Perhaps you meant s.split(s, "+");?
Yeah, I know it sounds like I'm nitpicking. But the compiler is much less forgiving than I. It's important to pay attention to these details. One is a char, and one is a String. Completely different things, as far as the compiler is concerned.
Wouter Oet wrote:Look at the link I provided again because it is clearly explained what the regex is for.
Hmmm, I think it's not nearly as clear as one might like. While I agree that we should look first to the official documentation, I think that the Java docs on regular expressions are less than clear. They are precise, and correct, but not clear. To most of the human race, I think.
Christophe Verré wrote:And be careful that "+" is a special symbol used in regular expressions.
I think this is a particularly useful hint. The "+" sign has special meaning in regular expressions. If you want to refer to a character that is really a simple "+" sign, without any special meaning in the regex world, you probably need to escape it. Try "\\+" instead. And see Backslashes, escapes, and quoting for more explanation.
Koren Washington wrote:yes i did, i put s.split(s, '+');
Unlikely - that would have produced a compiler error. Perhaps you meant s.split(s, "+");?
Actually, I think that this will compile. It is calling the split(String, int) method -- where the string itself will be passed in as the regex, and the ASCII for '+' will used as the limit.
Henry
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
2
posted
0
Ooo, you're right. Nice catch.
However, for Koren's benefit, I think everything I wrote after that still applies. Starting with:
Perhaps you meant s.split(s, "+")?
Try this first. And if it doesn't work, then try s.split("\\+").
Md Ibrahim
Greenhorn
Joined: Mar 24, 2010
Posts: 14
posted
0
Hi koren,
First of all, hey guys there's a wonderful way of splitting a String in java, have you all forgotten
StringTokenizer... Its THE ULTIMATE !!! chek out below
String YourString = "2.65+4.56";
ArrayList<String> aTokenCollector = new ArrayList<String>();
have you all forgotten StringTokenizer... Its THE ULTIMATE !!!
It's actually the old way of splitting strings, nothing ultimate here If you look carefully at the API, it says : "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead. "
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
2
posted
0
Christophe Verré wrote:
have you all forgotten StringTokenizer... Its THE ULTIMATE !!!
It's actually the old way of splitting strings, nothing ultimate here
Penultimate, perhaps.
The only nice thing about StringTokenizer is that, if you don't want to use a regex, it's cool that you don't have to escape special characters.