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?
 
Hi,

Try this

Code:
 public static String getNumbers(String str) {
        int sz = str.length();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == true) {
                buffer.append(str.charAt(i));
            }
        }
        return buffer.toString();
    }

output of the String "1test11" is "111";

Cheers
Venu
 
THANK YOU! I read about isDigit - but I couldn't figure out how to use it! That does just what I wanted!!! Thanks so much Venu!
 
Arghh, I can't avoid it, I'm a performace freak

Code:
 public static String getNumbers(String str) {
        int sz = str.length();
        StringBuffer buffer = new StringBuffer(sz);
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == true) {
                buffer.append(str.charAt(i));
            }
        }
        return buffer.toString();
    }

Cheers,

Dian
 
Hi,

LOL tought of the same after posting it here. good catch Dian.

Cheers
Venu

 
I want to make sure I understand what the difference is...

In Dian's posting - when the new StringBuffer object is created - it knows how big to create it? where as Venu's it didn't know - but since it didn't have a size - how big is the object created?
 
Other question - is the performance really that much better one way vs the other? How do you know...


I'm trying to learn Java so I apologize for any stupid question I may ask. I have read tutorials and books - but sometimes just hearing someone explain makes so much more sense than a book :)
 
When created without a size, a stringBuffer takes a default size of 16 chars. When you append chars to the buffer, as long as you add equals or less than 16 chars, all is OK; but if you add more, the buffer has to realocate memory to contain all added chars and that isn't optimized.

Water is not bad as long as it stays out human body ;-)
 
I know the size because the new String without numbers can't be larger than the original String.

As always, performance difference will depend on how long the String is and how many times you invoke that function. If you just do it once for a small string, you won't notice it, but if you do zillions of times with huge string, you will surely do.

If you want to become a freak like me, take a look at
Cheers,

Dian
 
To understand why Dian's example is a bit quicker than venur's example, you need to understand about memory allocation and the heap.

As you may or may bot know, Java stores objects on the "heap" (as opposed to the "stack"). The JVM needs to reserve space on the heap (in C, a "malloc", or C++ [like Java] through "new") - which is a fairly expensive call.

When you tell the StringBuffer object the size of the likely String to be held, it can reserve the space on the heap in just one call. But if you do not, each time you call append, the StringBuffer object must request more memory each time - thus slowing down your execution time.

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for your explanations...I think I'm understanding a little bit more. It's good stuff to know!

Is there a good book or tutorial out there that would teach more about performance enhancing stuff - or did you just kinda stumble upon this stuff as you went about your coding?
 
One command isn't always best. It's elegant, I give you that, but it also causes a regular expression evaluator to be created, which causes overhead. If this function was only going to be used once per JVM the REGEX overhead would be overkill, but if the application called the function 10000s of times the overhead wouldn't be noticed and ease of maintinance would prevale. It's all about context.

MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
... whereas I think the exact opposite of myenigmaself. If running it once only, the regex would be acceptable. If its gonna be run 10000 times, then a low memory, faster alternative should be chosen.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Talking about code optimization, I would have replaced:

if (Character.isDigit(str.charAt(i)) == true) {

by:

if (Character.isDigit(str.charAt(i))) {

Cheers,

Tom
 
tom62, I doubt that change is actually "optimized" at all - I
would expect that the compiler or interpreter evauluate those two expressions to be exactly the same in machine code.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top