gpt4 book ai didi

java - 每隔这么多字符留出空格

转载 作者:行者123 更新时间:2023-12-01 11:45:23 24 4
gpt4 key购买 nike

我知道这段代码会在每个字符之间添加一个空格...

String nospaces = encrypt.replaceAll("\\s+","");
StringBuilder sb = new StringBuilder();

for (char c: nospaces.toCharArray()) {
sb.append(c).append(" ");
}

System.out.println(sb.toString().trim());

如果您想在一定数量的字符后添加空格,例如5、你会怎么做?

最佳答案

您也可以在此使用replaceAll:

 String withSpaces = nospaces.replaceAll("(.{5})", "$1 ");

关于java - 每隔这么多字符留出空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29182374/

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