Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stripping numbers from strings

Status
Not open for further replies.
Oct 15, 2003
145
US
I have a string that has numbers and letters. I just want the numbers out of the string. Is there a way to just strip out numbers? Depending on the string the number could start at different positions.

Any thoughts?
 
Well, Tom talked about "code optimization". He didn't precise if it was bytecode optimisation or sourcecode optimisation ;-)

Water is not bad as long as it remains outside human body ;-)
 
optimization surely means "making it run quicker" - not "writing less lines of code" ?

--------------------------------------------------
Free Database Connection Pooling Software
 
My example doesn't run faster, but eliminates redundant code. For that reason I consider it to be an optimation.
 
My solution is fastest to understand, and in softwaredevelopment, the time to read the code by generations of programmers is expensive.

And the performance gets measurable by 100,000 if you do nothing time-intensive on the Strings.
But you wouldn't strip the Strings to numbers, without doing something on the Strings - would you?

seeking a job as java-programmer in Berlin:
 
... and less code means, in theory, less places for bugs to hide. I like the feel of Stefan's solution (provided it isn't gonna be run 1000s of times).

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
If you really want to leverage regular expressions and you're going to be using this function more than once then you should be employing the use of the Pattern class. Using String.replaceAll re-compiles the regular expression every time the method is called, which is hugely inefficient.

myenigmaself
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
True, but Wyald didn't specify that it would be used only once either. Generally a requirement like this is repeated, but not always.

myenigmaself
"If debugging is the process of removing bugs, then programming must be the process of putting them in." --Dykstra
 
And I thought I was a performance freak ...

Now I feel better.

I think I'd go for stefan's solution in 95% of the situations.

Cheers,

Dian
 
Stefan's solution is elegant - but I didn't totally understand what it was going to do - so I went another route. It looked to me that it would replace the numbers with blanks...I'm probably wrong since I didn't try it...but anyway I ended up with venur's solution. This does have to be repeated on several files...but the solution I'm using doesn't take long at all to complete.

Thank you all who have helped me. I do greatly appreciate it!
 
Code:
String result = source.replaceAll ("[^0-9]", "");
contains no blank, and means 'replace with nothing' aka 'delete'.
For a lot of replacements, myenigmaselfs solution with a precompiled pattern might be helpful.
I'm thinking of a small measurement...

[] makes a group, [0-9] means any digit, ^ negates the group, [^0-9] meaning anything except a digit.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top