• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Splitting Between String and Numbers

 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lets say we have
String a = "H2";
String b = "Cs432";

how can I split between the string and the integers? knowing that the string will always come first and the strings and integers will never be mixed (always string first then integers)

I know it must be simple enough, but I cant figure it out... thanks
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.split takes a regular expression as an argument. The regex you need is pretty simple so I'll leave that for you.
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know split() function, but no, the regex is not simple, the regex must be the first number, any number, but I don't want that number to be cut off, I want to include it in the integer...
could you please be more specific?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12101
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you tried to write a regex for this? we try to not just hand out answers, but to help you figure it out. show what you've done, what you've tried, and tell us what it did/why it didn't work. we can then guide you to the correct solution.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you know that it's always non-numeric followed by numeric, all you need to do is search for the first number, then split around that index. That should make it quite easy.
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved it... i didnt use split()


is it good ?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People have already told you to use the String#split() method with a regular expression. You need to go through the regular expressions part of the Java Tutorials. You can find a regular expression which will split on digits, or you can use a matching method, find out the index of the last letter, then the digits follow. Or use a matching method to find out the index of the first digit.

[edit]Corrected spelling of "idnex."[/edit]
[ May 27, 2008: Message edited by: Campbell Ritchie ]
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I don't think String.split will help here. That method "eats" the substrings that match its regular expression. That's not what Sam needs.

Originally posted by Sam Benry:
solved it... i didnt use split()

is it good ?

There are some issues I have with this:
1) NEVER EVER just ignore any exception. The least you should do is print it or its stack trace. Or, in your case, document why you are ignoring it.

2) strChar is an array of Strings, each of length one. You only use each element once, during the loop. This isn't efficient. Don't store it as an array but as a String - only store the last needed value.


Furthermore, this code creates quite a lot of substrings, leading to an explosion of object creations. As I said, finding the first number can help you:
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I think you are right, Rob that there are easier ways than String#split.

Sam, are you aware of the methods in the Character class which tell what sort of char you are dealing with?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
To be honest, I don't think String.split will help here. That method "eats" the substrings that match its regular expression.


Unless you are using a "lookaround" aka "zero width assertion", I'd guess: http://www.regular-expressions.info/lookaround.html
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie
I was not aware of this method, this simplifies things a lot. Thanks

@Rob Prime
nice code, I have a question, would it be good to add


this way, as soon as the number is found the method returns. Is this better, worse or makes no difference ?
Thanks
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about just checking if the char at the current position is a digit? trying Integer.parseInt() and catching the exception it throws if the char is not a digit is a pretty odd way of checking.

Maybe this helps you (Java can compare chars in the style of numbers):

This works because the digits 0 - 9 occupy a closed interval in ASCII.
Another option is this:

This is a little slower than the method above, but has the advantage that you can handle non-continuous sets of characters as well.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would work, Guido, but I think Sam is off to the character class, whihc has all sorts of useful methods for that sort of thing.
 
Guido Sautter
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using the Character class is surely the more elegant way of doing it, but I think the char interval comparison is faster, for it does not involve method invokations, but only primitive type comparison ...
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Benry:
@Rob Prime
nice code, I have a question, would it be good to add


this way, as soon as the number is found the method returns. Is this better, worse or makes no difference ?
Thanks

I wouldn't just parse your one character - you want to parse the entire remainder, to make sure it is completely numeric. That's what I did, since s.substring(i) is equal to s.substring(i, s.length()).

I also started using Object[] instead of String[] so you can store the Integer object. My guess is that you will parse the String back to an Integer or int just a bit later, aren't you? My code will save the double parsing.

You are completely correct about returning immediately. You could also call "break" which immediately will jump to the end of the loop, therefore also returning result.

Originally posted by Guido Sautter:
Using the Character class is surely the more elegant way of doing it, but I think the char interval comparison is faster, for it does not involve method invokations, but only primitive type comparison ...

Although Character.isDigit is a bit slower, I doubt you can even notice unless you're checking the difference in nanoseconds. It is efficient enough to use it regularly. Another benefit is that it is easier to read. The method name explains exactly what it is trying to do - checking if a Character is (a) Digit.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:

Although Character.isDigit is a bit slower, I doubt you can even notice unless you're checking the difference in nanoseconds. It is efficient enough to use it regularly. Another benefit is that it is easier to read. The method name explains exactly what it is trying to do - checking if a Character is (a) Digit.
It also has the advantage of having been tested by thousands of people worldwide, so we can be reasonably confident it is reliable.
 
Alan Moore
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
Unless you are using a "lookaround" aka "zero width assertion", I'd guess: http://www.regular-expressions.info/lookaround.html


Right:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic