?
Solved

How do I extract a number from a Java string?

Posted on 2012-04-05
5
Medium Priority
?
345 Views
Last Modified: 2012-04-06
I have the following string in Java:

"Send data to port 2112 with this entire message intact"

What is a good way to extract the number from the string and place "2112" in an integer?

I know some "brute force or hard" ways to do this but I am looking for something that uses good Java programming techniques.

What I am actually doing here is testing that ports are available---so i send a message to a server on port 2112 and tell it to send me back the same message on the specified port.
0
Comment
Question by:Christopher Schene
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 37814599
If there's only one:

String s = "Send data to port 2112 with this entire message intact";
String n = s.replaceAll("\\D", "");
int i = Integer.parseInt(n);

Open in new window

0
 

Author Comment

by:Christopher Schene
ID: 37814770
Works great! Thanks!
0
 

Author Closing Comment

by:Christopher Schene
ID: 37814772
Fast response!
0
 

Expert Comment

by:kamalakardandu
ID: 37814982
Good Answer....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37815564
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Please see preceding article here: http://www.experts-exchange.com/Networking/Operating_Systems/A_11209-Root-Bridge-Election.html Figure 1 After Root Bridge has been elected, then what?..... Let's start by defining a Root Port in la…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question