Reputation: 2071
I can build the string like this:
String str = "Phone number %s just texted about property %s";
String.format(str, "(714) 321-2620", "690 Warwick Avenue (679871)");
//Output: Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)
What I want to achieve is reverse of this. Input will be following string
Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)
And I want to retrieve, "(714) 321-2620" & "690 Warwick Avenue (679871)" from input
Can any one please give pointer, how to achieve this in Java or Android?
Thank you in advance.
Upvotes: 1
Views: 1349
Reputation: 70931
Use regular expressions:
String input = "Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)";
Matcher m = Pattern.compile("^Phone number (.*) just texted about property (.*)$").matcher(input);
if(m.find()) {
String first = m.group(1); // (714) 321-2620
String second = m.group(2); // 690 Warwick Avenue (679871)
// use the two values
}
Complete working code:
import java.util.*;
import java.lang.*;
import java.util.regex.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String input = "Phone number (714) 321-2620 just texted about property 690 Warwick Avenue (679871)";
Matcher m = Pattern.compile("^Phone number (.*) just texted about property (.*)$").matcher(input);
if(m.find()) {
String first = m.group(1); // (714) 321-2620
String second = m.group(2); // 690 Warwick Avenue (679871)
System.out.println(first);
System.out.println(second);
}
}
And the link on ideone.
Upvotes: 7
Reputation: 6319
This is easy, and at the same time pretty hard.
Basically, you can easily just use String.split() to split up the string in parts on regular expressions or first appearances of characters.
You will however, need to have a clear pattern to detect in which the phone number and the address are. This depends on your own definition of the possibilities of these info's.
Upvotes: 0