Possible Duplicate:
string split in java
I have this Key - Value
, and I want to separate them from each other and get return like below:
String a = "Key"
String b = "Value"
so whats the easiest way to do it ?
split("\\s+") will split the string into string of array with separator as space or multiple spaces. \s+ is a regular expression for one or more spaces.
String[] tok = "Key - Value".split(" - ", 2);
// TODO: check that tok.length==2 (if it isn't, the input string was malformed)
String a = tok[0];
String b = tok[1];
The " - "
is a regular expression; it can be tweaked if you need to be more flexible about what constitutes a valid separator (e.g. to make the spaces optional, or to allow multiple consecutive spaces).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With