gpt4 book ai didi

Java:如何在反转分割字后将分隔符读回字符串?

转载 作者:行者123 更新时间:2023-12-02 00:58:12 25 4
gpt4 key购买 nike

在反转拆分单词后,我不确定如何重新添加分隔符。

String fileContent = "def  mnop.STU";
String delimiter = "[^a-zA-Z0-9']+";
String[] splitWords = fileContent.split(delimiter);
StringBuilder stringBuilder = new StringBuilder();
for (String word : splitWords) {
StringBuilder output = new StringBuilder(word).reverse();
stringBuilder.append(output);
}
StringJoiner joiner = new StringJoiner(delimiter);
joiner.add(stringBuilder);
System.out.println(joiner.toString());

当前输出:fedponmUTS

所需输出:fed ponm.UTS

最佳答案

这不是很优雅,但是你能做这样的事情吗?

for (String word : splitWords) 
{
int idx = fileContent.indexOf(word, stringBuilder.length());
String delim = fileContent.substring(stringBuilder.length(), idx);
stringBuilder.append(delim);

StringBuilder output = new StringBuilder(word).reverse();
stringBuilder.append(output);
}

输出:

fed  ponm.UTS

关于Java:如何在反转分割字后将分隔符读回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61053304/

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