aspose file tools*
The moose likes Beginning Java and the fly likes Splitting Between String and Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Splitting Between String and Numbers" Watch "Splitting Between String and Numbers" New topic
Author

Splitting Between String and Numbers

Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
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

Joined: Aug 05, 2005
Posts: 3681
    
  16
String.split takes a regular expression as an argument. The regex you need is pretty simple so I'll leave that for you.


Joanne
Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
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

Joined: Oct 02, 2003
Posts: 11419
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
solved it... i didnt use split()


is it good ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39425
    
  28
No
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39425
    
  28
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Oct 13, 2005
Posts: 39425
    
  28
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

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sam Benry
Ranch Hand

Joined: Mar 21, 2008
Posts: 89
@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

Joined: Dec 22, 2004
Posts: 142
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

Joined: Oct 13, 2005
Posts: 39425
    
  28
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

Joined: Dec 22, 2004
Posts: 142
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

Joined: Oct 27, 2005
Posts: 19720
    
  20

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

Joined: Oct 13, 2005
Posts: 39425
    
  28
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

Joined: May 06, 2004
Posts: 262
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Splitting Between String and Numbers