gpt4 book ai didi

java - 在Java中,如何在不删除字符串中任何字符的情况下分割字符串?

转载 作者:行者123 更新时间:2023-12-01 18:09:57 25 4
gpt4 key购买 nike

我有一个像这样的字符串:

String str = "How {$can} {$we} split this {$string}?";

我必须把它分成几个 block :

["How ","{$can}"," ","{$we}"," split this ","{$string}","?"]

最佳答案

您可以使用此拆分:

String str = "How {$can} {$we} split this {$string}?";

String[] arr = str.split("(?=\\{)|(?<=\\})");

RegEx Demo

正则表达式分解:

(?=\\{)   # if next char is {
| # regex alternation
(?<=\\}) # if previous char is }

Read about look arounds in regex

关于java - 在Java中,如何在不删除字符串中任何字符的情况下分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33767071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com