我有一个字符串
$key1={331015EA261D38A7}
$key2={9145A98BA37617DE}
$key3={EF745F23AA67243D}
如何根据“$”和“下一行”元素拆分每个键? 然后把它放在Arraylist中?
输出应如下所示: 数组列表[0]:
$key1={331015EA261D38A7}
ArrayList的[1]:
$key2={9145A98BA37617DE}
ArrayList的[2]:
$key3={EF745F23AA67243D}
答案 0 :(得分:3)
如果您只想按新行分割,则只需:
yourstring.split("\n"):
答案 1 :(得分:0)
yourstring.split(System.getProperty( “line.separator”));
答案 2 :(得分:0)
不要拆分,搜索您的String
键值对:
\$(?<key>[^=]++)=\{(?<value>[^}]++)\}
例如:
final Pattern pattern = Pattern.compile("\\$(?<key>[^=]++)=\\{(?<value>[^}]++)\\}");
final String input = "$key1={331015EA261D38A7}\n$key2={9145A98BA37617DE}\n$key3={EF745F23AA67243D}";
final Matcher matcher = pattern.matcher(input);
final Map<String, String> parse = new HashMap<>();
while (matcher.find()) {
parse.put(matcher.group("key"), matcher.group("value"));
}
//print values
parse.forEach((k, v) -> System.out.printf("Key '%s' has value '%s'%n", k, v));
输出:
键'key1'的值为'331015EA261D38A7' 键'key2'的值为'9145A98BA37617DE' 键'key3'的值为'EF745F23AA67243D'