如何从char数组中删除彼此相邻的2个或多个空格

时间:2022-04-14 21:42:27
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';

            }

        }

    }

}

I need to reduce all sequences of 2 or more spaces to 1 space within the characters array. If any spaces are removed then the same number of Null character '\u0000' will fill the elements at the end of the array. My code does not remove 4 spaces when there are 5 spaces in between. Such as {'e','','','','','','4'}

我需要在字符数组中将2个或更多空格的所有序列减少到1个空格。如果删除了任何空格,则相同数量的Null字符'\ u0000'将填充数组末尾的元素。当中间有5个空格时,我的代码不会删除4个空格。比如{'e','','','','','','4'}

2 个解决方案

#1


2  

How about converting it to String, doing some regex and back to char array?

如何将它转换为String,做一些正则表达式并返回char数组?

new String(characters).replaceAll("[ ]+", " ").toCharArray()

EDIT: Ok, I see you're actually not returning the array, but modifying the original array. Then you could use a simple loop

编辑:好的,我看到你实际上没有返回数组,而是修改原始数组。然后你可以使用一个简单的循环

String s = new String(characters).replaceAll("[ ]+", " ");
for (int i = 0; i < characters.length; i++) {
    characters[i] = (i < s.length() ? s.charAt(i) : '\u0000');
}

#2


1  

different variant, to show you how it should work with simple loops:

不同的变体,向您展示它应该如何使用简单的循环:

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;
        }
    }
}

#1


2  

How about converting it to String, doing some regex and back to char array?

如何将它转换为String,做一些正则表达式并返回char数组?

new String(characters).replaceAll("[ ]+", " ").toCharArray()

EDIT: Ok, I see you're actually not returning the array, but modifying the original array. Then you could use a simple loop

编辑:好的,我看到你实际上没有返回数组,而是修改原始数组。然后你可以使用一个简单的循环

String s = new String(characters).replaceAll("[ ]+", " ");
for (int i = 0; i < characters.length; i++) {
    characters[i] = (i < s.length() ? s.charAt(i) : '\u0000');
}

#2


1  

different variant, to show you how it should work with simple loops:

不同的变体,向您展示它应该如何使用简单的循环:

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;
        }
    }
}