There is a
String temp = "M0-1"
How to convert this String to a String[] ?
If you output the new String array, the result should be:
String[] newStringArray;
newStringArray[0] = M;
newStringArray[1] = 0;
newStringArray[2] = -1;
The last element of the new String array should be “-1”, which means “-1” will be considered as a entirety, but not “-” and “1” separately.
I tried to use String.split(“”) parse this string, but only got a String array like this:
String[] newStringArray;
newStringArray[0] = M;
newStringArray[1] = 0;
newStringArray[2] = -;
newStringArray[3] = 1;
UPDATE: String 56 should be [5, 6], and -10 should be [-1, 0]
This
split()will do it:It works by splitting when the previous character is not a
-That odd-looking regex is called a look-behind. Its syntax is
(?<=regex), which is a non-capturing assertion that the preceding input matches the specified regex, which in this case is[^-], which means “any character that isn’t a minus”.There are 4 versions of this kind of assertion:
(?=regex)is a positive look ahead, meaning the next input must matchregex(?!regex)is a negative look ahead, meaning the next input must not matchregex(?<=regex)is a positive look behind, meaning the preceeding input must matchregex(?<!regex)is a negative look behind, meaning the nepreceedingxt input must not matchregexHere’s a test:
Output: