I want to find integer value from a string. For example, given a String like "Ahmedabad to Gandhinagar Distance:29km(about 31 mins)"
I want to fetch only 29 from the given String because i want to compare this 29 kilometers to other kilometers.
I want to find integer value from a string. For example, given a String like "Ahmedabad to Gandhinagar Distance:29km(about 31 mins)"
I want to fetch only 29 from the given String because i want to compare this 29 kilometers to other kilometers.
In C#, you can use a Regex.Match to pull out substrings if you use groups in the regex (note this is not tested...). There is probably a similar mechanism in Java, but I don't know it off the top of my head:
var myString = @"Ahmedabad to Gandhinagar Distance:29km(about 31 mins)";
var myRegex = @".*:(\d*)km.*";
var match = Regex.Match(myString, myRegex);
if (match.Success)
{
// match.Groups contains the match "groups" in the regex (things surrounded by parentheses)
// match.Groups[0] is the entire match, and in this case match.Groups[1] is the km value
var km = match.Groups[1].Value;
}
Use this regex
/[:](\d+)/
and you'll have the match as 29. Here is a rubular link with your example.