Reputation: 7879
I have a string such as:
file = "UserTemplate324.txt"
I'd like to extract "324". How can I do this without any external libraries like Apache StringUtils?
Upvotes: 0
Views: 85
Reputation: 424973
Assuming you want "the digits just before the dot":
String number = str.replaceAll(".*?(\\d+)\\..*", "$1");
This uses regex to find and capture the digits and replace the entire input with them.
Of minor note is the use of a non-greedy quantifier to consume the minimum leading input (so as not to consume the leading part of the numbers too).
Upvotes: 3
Reputation: 328568
If you want to exclude the non-digit characters:
String number = file.replaceAll("\\D+", "");
\\D+
means a series of one or more non digit (0-9) characters and you replace any such series by ""
i.e. nothing, which leaves the digits only.
Upvotes: 2