[英]How to add a space between items in a string
How can I add a space between distinct items in a string.如何在字符串中的不同项目之间添加空格。 Given the string for example:
给定字符串例如:
"22@((12@45)&14)"
How can I make it a string separated by spaces like this:如何使它成为一个由空格分隔的字符串,如下所示:
"22 @ ( ( 12 @ 45 ) & 14 )"
Note that numbers greater than 9 don't have a space in between their digits请注意,大于 9 的数字之间没有空格
You can try with this, it works, and it will give you a base for modifications if you need them:你可以试试这个,它可以工作,如果你需要,它会给你一个修改的基础:
public static void main(String[] args) {
String someString = "22@((12@45)&14)";
int length = someString.length();
StringBuilder result = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = someString.charAt(i);
result.append(c);
if (!Character.isDigit(c) || (i < length-1 && !Character.isDigit(someString.charAt(i+1)))){
result.append(" ");
}
}
System.out.println(result.toString());
}
If you have only integer numbers, it can be implemented as following:如果您只有 integer 编号,则可以按以下方式实现:
private static String formatExpression(String input) {
return String.join(" ", input.split("((?<=\\D)|(?=\\D))"));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.