简体   繁体   English

就地删除某些字符

[英]Remove certain chars in-place

As far as I know, in C we can modify a char-array in-place and then append the \0 to make the char-array shorter.据我所知,在 C 我们可以就地修改一个字符数组,然后 append \0使字符数组更短。 I am wondering how that's done in Java.我想知道这是如何在 Java 中完成的。

Assume we want to remove the spaces from a char-array in Java.假设我们要从 Java 中的字符数组中删除空格。


char[] str = new String("cat love dogs").toCharArray();

for(int i = 0; i < str.length; i++) {
    if(str[i] == ' ')
        str[i] = '';
}

This does not seem to work.这似乎不起作用。

EDIT: I already know about built-in functions to replace some chars with another in a string.编辑:我已经知道用字符串中的另一个字符替换一些字符的内置函数。 I am not asking that.我不是在问那个。 Curious to modify a char-array in-place with O(1) extra space.好奇用O(1)额外空间就地修改字符数组。

well the best solution i can see is remove all spaces from your string first then get the chars from it:好吧,我能看到的最好的解决方案是先从字符串中删除所有空格,然后从中获取字符:

 String myString = new String("cat love dogs").replaceAll("\\s+", ""); 
     // \\s+ is the regex for sapces 
    // myString becomes => catlovedogs
 char[] str = myString.toCharArray(); 

As String is immutable you may use a StringBuilder class:由于 String 是不可变的,您可以使用 StringBuilder class:

StringBuilder sb = new StringBuilder("cat love dogs");

for(int i = 0; i < sb.length(); i++) {
        if(sb.charAt(i) == ' ') {
            sb.deleteCharAt(i);
            i--;
        }
    }

char[] str = sb.toString().toCharArray();

Now as far as space is concerned String class in Java is immutable!现在就空间而言,Java 中的字符串 class 是不可变的!

Thus you cannot do anything in place there.因此你不能在那里做任何事情。

But you can use StringBuilder which is immutable.但是您可以使用不可变的 StringBuilder。

Code:代码:

StringBuilder stringBuilder = new StringBuilder("cat love dogs");
for(int i = 0; i < stringBuilder.length(); i++) {
    if (sb.charAt(i) == ' ')
        sb.deleteCharAt(i);
}
char[] str = stringBuilder.toString().toCharArray();

Or if you want to do it just using the char array itself its not possible as char arrays are not dynamic by themselves.或者,如果您只想使用 char 数组本身来做到这一点,这是不可能的,因为 char arrays 本身不是动态的。

But here is what you could do:但这是你可以做的:

char[] str = new String("cat love dogs").toCharArray();
int last = 0;
for(int i = 0; i < str.length; i++) {
    if(str[i] == ' '){
        str = ArrayUtils.remove(str, i);
    }
}
System.out.println(new String(str));
System.out.println(new String(str).length());

Output: Output:

catlovedogs
11

For for this you need to import org.apache.commons.lang3.ArrayUtils为此,您需要导入org.apache.commons.lang3.ArrayUtils

If you are not concerned about space:如果您不关心空间:

In Java we can just set the character value to 0!在 Java 中我们可以将字符值设置为 0!

Here is the modified code:这是修改后的代码:

char[] str = new String("cat love dogs").toCharArray();
for(int i = 0; i < str.length; i++) {
    if(str[i] == ' ')
        str[i] = 0;
}
System.out.println(new String(str));

Here is the output:这是 output:

catlovedogs

I think the best way to do this is by creating a duplicate array of chars, where you copy every character besides the spaces and then return the new array.我认为最好的方法是创建一个重复的字符数组,在其中复制除空格之外的每个字符,然后返回新数组。 Here is how the code might look like:下面是代码的样子:

        char[] str = new String("cat love dogs").toCharArray();
        List<Character> duplicate = new ArrayList<>();
        
        for(int i = 0; i < str.length; i++) {
            if(str[i] != ' ') {
                duplicate.add(str[i]);
            }
                
        } 

it's better to use ArrayList because you don't really know the final size of the array and this way you can go only as big as needed.最好使用 ArrayList 因为您并不真正知道数组的最终大小,这样您就可以只根据需要使用 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM