Reputation: 265
I need to split an Alphanumeric String and split the alphabets and numbers. For example, if I have a string like: "Room 502", I need to get the number 502 as a separate string to "Room". Could someone please help me with this?
I know it's probably something so simple, but all the examples I have seen don't seem to do what I need.
Upvotes: 1
Views: 3447
Reputation: 668
For this example, you could just split on the space character, since the string is literally Room 502
and you want Room
and 502
, you could just do:
String example = "Room 502";
String[] components = string.split(" ");
If what you are trying to split is a bit more complex, for example Room502isnice
as suggested in the comments, we have to use something called a regular expression, or regex.
The following regular expression would split the string Room502isnice
into Room
, 502
and isnice
:
([A-z]+)|(\d+)
I'm not an expert in Java so I'm not 100% sure how it would be applied in Java.
See this regexr example: http://regexr.com/399uu
Upvotes: 2