我想每隔几个字符在字符串中添加一个分隔符,现在的问题是位数不相同,即不是每隔N个字符后 因此,如果字符串为“ 111111222222223333333444444444455566” 我希望输出像“ 111111.22222222.3.4444444.555.66”
所以我目前正在使用以下子字符串;
//Please ignore the substring lengths shown below...it is just an example
String s = "111111222222223333333444444455566";
String s1 = s.substring(0, 5);
String s2 = s.substring(5, 15);
String s3 = s.substring(15, 20);
String dashedString = s1 + "-" + s2 + "-" + s3;
但是我的问题是,是否有更好的方法可以实现相同的目标?
答案 0 :(得分:2)
您只需遍历字符串的字符并检测当前相同字符的字符序列何时更改。
public static void main(String args[]) {
String input = "11111122222333444";
String result = "";
char currentChar = input.charAt(0);
for(int i=0; i<input.length(); i++) {
if(currentChar != input.charAt(i)) {
result += "." + input.charAt(i);
currentChar = input.charAt(i);
}else {
result += input.charAt(i);
}
}
System.out.println(result);
}
答案 1 :(得分:0)
在固定分隔符位置的情况下,您可以将StringBuilder
与insert(...)
方法一起使用:
String s = "111111222222223333333444444455566";
String dashedString = new StringBuilder(s)
.insert(6, '-')
.insert(15, '-')
.insert(23, '-')
.insert(31, '-')
.insert(35, '-')
.toString();
给您111111-22222222-3333333-4444444-555-66
答案 2 :(得分:0)
好答案取决于哪种更好的方法。下面列出的三种方法都具有相同的参数和输出。
参数:
String source = "111111222222223333333444444455566";
String delimiter = "-";
int[] delimiterIdx = new int[] { 6, 14, 21, 28, 31 };
输出:
111111-22222222-3333333-4444444-555-66
// 1
public static String joinStringJoiner(final String source, final String delimiter,
final int[] delimiterIdx) {
final StringJoiner joiner = new StringJoiner(delimiter);
int prev = 0;
for (int i = 0; i < delimiterIdx.length; i++) {
joiner.add(source.subSequence(prev, delimiterIdx[i]));
prev = delimiterIdx[i];
}
joiner.add(source.subSequence(prev, source.length()));
return joiner.toString();
}
// 2
public static String joinStringBuilder(final String source, final String delimiter,
final int[] delimiterIdx) {
final StringBuilder stringBuilder = new StringBuilder(source);
for (int i = 0; i < delimiterIdx.length; i++) {
stringBuilder.insert(delimiterIdx[i] + i, delimiter);
}
return stringBuilder.toString();
}
// 3
public static String joinSubstring(final String source, final String delimiter,
final int[] delimiterIdx) {
int prev = 0;
String result = "";
for (int i = 0; i < delimiterIdx.length; i++) {
result += source.substring(prev, delimiterIdx[i]);
result += delimiter;
prev = delimiterIdx[i];
}
result += source.substring(prev, source.length());
return result;
}
我运行了caliper个基准测试并获得了结果:
Trial Report (1 of 6):
Experiment {instrument=allocation, benchmarkMethod=benchmarkStringJoiner, vm=default, parameters={}}
Results:
bytes(B): min=899.88, 1st qu.=902.63, median=903.82, mean=903.48, 3rd qu.=904.74, max=905.88
objects: min=22.07, 1st qu.=22.08, median=22.10, mean=22.11, 3rd qu.=22.14, max=22.17
Trial Report (2 of 6):
Experiment {instrument=allocation, benchmarkMethod=benchmarkStringBuilder, vm=default, parameters={}}
Results:
bytes(B): min=276.63, 1st qu.=277.60, median=278.83, mean=278.67, 3rd qu.=279.76, max=280.33
objects: min=4.08, 1st qu.=4.11, median=4.13, mean=4.13, 3rd qu.=4.16, max=4.19
Trial Report (3 of 6):
Experiment {instrument=allocation, benchmarkMethod=benchmarkSubstring, vm=default, parameters={}}
Results:
bytes(B): min=2569.33, 1st qu.=2574.14, median=2574.48, mean=2574.47, 3rd qu.=2576.24, max=2576.99
objects: min=56.07, 1st qu.=56.09, median=56.12, mean=56.12, 3rd qu.=56.14, max=56.17
Trial Report (4 of 6):
Experiment {instrument=runtime, benchmarkMethod=benchmarkStringBuilder, vm=default, parameters={}}
Results:
runtime(ns): min=663.96, 1st qu.=689.33, median=727.41, mean=750.09, 3rd qu.=815.34, max=889.49
Trial Report (5 of 6):
Experiment {instrument=runtime, benchmarkMethod=benchmarkStringJoiner, vm=default, parameters={}}
Results:
runtime(ns): min=1391.68, 1st qu.=1428.84, median=1441.44, mean=1450.81, 3rd qu.=1488.55, max=1502.96
Trial Report (6 of 6):
Experiment {instrument=runtime, benchmarkMethod=benchmarkSubstring, vm=default, parameters={}}
Results:
runtime(ns): min=1771.43, 1st qu.=3097.61, median=3142.81, mean=3021.71, 3rd qu.=3214.30, max=3354.84
因此,我想在这种情况下,“更好的方法”是joinStringBuilder
方法(2)。