Solved

How do I extract a number from a Java string?

Posted on 2012-04-05
5
343 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 500 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

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

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

The article explains the protocols and technology which is involved when two computers on different TCP/IP networks communicate with each other. In the diagram, a router is used to segregate two networks. The networks are 192.168.1.0/24 and 192…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

689 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