1
比方說,我有一個字符串,說"Hello123"
,我怎麼能將它們分開成爲s[0] = "Hello", s[1] = "123"
?我希望使用s.split()
,但我不知道要在參數/參數中放置什麼。單獨的數字和字母
比方說,我有一個字符串,說"Hello123"
,我怎麼能將它們分開成爲s[0] = "Hello", s[1] = "123"
?我希望使用s.split()
,但我不知道要在參數/參數中放置什麼。單獨的數字和字母
你可以使用正則表達式:
String[] splitArray = subjectString.split(
"(?x) # verbose regex mode on \n" +
"(?<= # Assert that the previous character is... \n" +
" \\p{L} # a letter \n" +
") # and \n" +
"(?= # that the next character is... \n" +
" \\p{N} # a digit. \n" +
") # \n" +
"| # Or \n" +
"(?<=\\p{N})(?=\\p{L}) # vice versa");
分裂
psdfh123sdkfjhsdf349287
到
psdfh
123
sdkfjhsdf
349287
首先,找到一個子那就是字符1-9之一,再拆在那個位置。 – Randy