Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I extract a number from a Java string?

Posted on 2012-04-05
5
Medium Priority
?
347 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Configuring network clients can be a chore, especially if there are a large number of them or a lot of itinerant users.  DHCP dynamically manages this process, much to the relief of users and administrators alike!
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

636 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