• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Split a string and add the contents

 
Koren Washington
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at String.split
 
Koren Washington
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so lets say my string was s, how would i split it at the plus sign?

s.split(s, '+');

?
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koren Washington wrote:so lets say my string was s, how would i split it at the plus sign?


Did you read the link that was provided?

Henry
 
Koren Washington
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i did, i put s.split(s, '+');

im just wondering how can i make it stop at the plus sign? do i have to declare a separate string for plus then do

String plus = "+";
s.split(plus);

is that what the regex is referring to?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, "stop at the plus sign"?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non void methods return values. And be careful that "+" is a special symbol used in regular expressions.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21515
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>();

StringTokenizer newTokenizer = new StringTokenizer(YourString ,"+");

while (newTokenizer.hasMoreTokens()){

aTokenCollector.add(newTokenizer.nextToken());

}

Now your aTokenCollector Arrray list contents will be

2.65
4.56

What more you need, if you want to add these two as numeric values, Type cast them to int
using Integer.parseInt("2.65") and do it.


Hey koren...hope you would love using Tokenizer,,,do try and write back..take care

By,
Ibrahim




 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Other than that, it kind of sucks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic