public static void removeDuplicateSpaces(char[] characters) {
int dupCount = 0;
for (int i = 0; i < characters.length; i++) {
if (characters[i] == ' ' && characters[i + 1] == ' ') {
dupCount++;
for (int j = i; j < characters.length - 1; j++) {
characters[j] = characters[j + 1];
}
if ((characters[i] == ' ' && characters[i + 1] == ' ')) {
dupCount++;
for (int j = i; j < characters.length - 1; j++) {
characters[j] = characters[j + 1];
}
dupCount++;
}
for (int add = characters.length - 1; add >
characters.length- dupCount; add--) {
characters[add] = '\u0000';
}
}
}
}
我需要将2个或更多空格的所有序列减少到1个空格 字符数组。如果删除任何空格,则相同的数字 空字符'\ u0000'将填充结尾处的元素 阵列。 当中间有5个空格时,我的代码不会删除4个空格。 例如{'e','','','','','','4'}
答案 0 :(得分:2)
如何将它转换为it("should get data", function(done){
service.initialize(1234).then(function(){
console.log("I've been resolved!");
expect(mockAsyncService.getData).toHaveBeenCalledWith(1234);
done();
});
});
,执行一些正则表达式并返回char数组?
String
编辑:好的,我看到你实际上没有返回数组,而是修改原始数组。然后你可以使用一个简单的循环
new String(characters).replaceAll("[ ]+", " ").toCharArray()
答案 1 :(得分:1)
不同的变体,向您展示如何使用简单的循环:
public static void removeDuplicateSpaces(final char[] characters) {
for (int i = 0; i < characters.length; i++) {
while (characters[i] == ' ') { // while current symbol is space
for (int j = (i + 1); j < characters.length; j++)
characters[j - 1] = characters[j]; // shift the rest of array
characters[characters.length - 1] = 0;
}
}
}