我有一个这样的字符串:
String str = "How {$can} {$we} split this {$string}?";
我必须把它分成几个块:
["How ","{$can}"," ","{$we}"," split this ","{$string}","?"]
答案 0 :(得分:4)
您可以使用此拆分:
String str = "How {$can} {$we} split this {$string}?";
String[] arr = str.split("(?=\\{)|(?<=\\})");
RegEx分手:
(?=\\{) # if next char is {
| # regex alternation
(?<=\\}) # if previous char is }