Just use the appropriate method:
String#split()
.String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
Note that this takes a regular expression, so remember to escape special characters if necessary, e.g. if you want to split on period .
which means "any character" in regex, use either split("\\.")
or split(Pattern.quote("."))
.To test beforehand if the string contains a
-
, just use String#contains()
.if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Comments
Post a Comment