简体   繁体   English

从字符串中删除空格、数字和特殊字符

[英]Removing spaces, numbers and special characters from a string

I am writing a function to remove spaces from a String passed as an argument.我正在编写一个函数来从作为参数传递的字符串中删除空格。

This code works:此代码有效:

public static String removeSpecialChars(String str) {
    String finalstr = "";
    char[] arr = str.toCharArray();
    char ch;
    for (int i = 0; i < arr.length; i++) {
        ch = arr[i];
        if (Character.isLetter(ch))
            finalstr = finalstr.concat(String.valueOf(ch));
        else
            continue;
    }
    return finalstr;
}

And the output for the String 'hello world!'以及字符串 'hello world!' 的输出is as follows:如下:

helloworld

But this one doesn't:但这个没有:

public static String removeSpecialChars(String str) {
    char[] arr = str.toCharArray();
    char[] arr2 = new char[str.length()];
    char ch;
    for (int i = 0; i < arr.length; i++) {
        ch = arr[i];
        if (Character.isLetter(ch))
            arr2[i] = ch;
    }
    return String.valueOf(arr2);
}

Output:输出:

hello world

I get the same String back as an output, but only the exclamation mark is removed.我得到相同的字符串作为输出,但只删除了感叹号。 What could be the reason for this?这可能是什么原因? Any help would be appreciated.任何帮助,将不胜感激。

A char value is just a numeric value in the range 0 to 2¹⁶−1. char值只是 0 到 2¹⁶−1 范围内的数值。 In hexadecimal (base 16), we write that as 0000 to ffff.在十六进制(基数 16)中,我们将其写为 0000 到 ffff。

So, knowing that each char array is a sequence of numeric values, let's look at the state of each array as your program proceeds.因此,知道每个char数组都是一个数值序列,让我们在程序进行时查看每个数组的状态。 (I'm showing each value as two hex digits, rather than four, for brevity, since they are all in the range 00–ff.) (为简洁起见,我将每个值显示为两个十六进制数字,而不是四个,因为它们都在 00-ff 范围内。)

char [] arr = str.toCharArray();

// [ 68 65 6c 6c 6f 20 77 6f 72 6c 64 21 ]
// (UTF-16 values for the characters in "hello world!")

char [] arr2 = new char[str.length()];

// [ 00 00 00 00 00 00 00 00 00 00 00 00 ]
// (uninitialized arrays are always initialized with zeroes)

char ch;
for (int i = 0; i < arr.length; i++) {
    ch = arr[i];
    if (Character.isLetter(ch))
        arr2[i] = ch;
}

// arr2 after first loop iteration:
// [ 68 00 00 00 00 00 00 00 00 00 00 00 ]

// arr2 after second loop iteration:
// [ 68 65 00 00 00 00 00 00 00 00 00 00 ]

// arr2 after third loop iteration:
// [ 68 65 6c 00 00 00 00 00 00 00 00 00 ]

// arr2 after fourth loop iteration:
// [ 68 65 6c 6c 00 00 00 00 00 00 00 00 ]

// arr2 after fifth loop iteration:
// [ 68 65 6c 6c 6f 00 00 00 00 00 00 00 ]

// During sixth loop iteration,
// the if-condition is not met, so arr2[6]
// is never changed at all!
// [ 68 65 6c 6c 6f 00 00 00 00 00 00 00 ]

// arr2 after seventh loop iteration:
// [ 68 65 6c 6c 6f 00 77 00 00 00 00 00 ]

// During twelfth and final loop iteration,
// the if-condition is not met, so arr2[11]
// is never changed at all!
// [ 68 65 6c 6c 6f 00 77 6f 72 6c 64 00 ]

I don't know how you're examining the returned string, but here is what's actually in it:我不知道你是如何检查返回的字符串的,但这里是其中的实际内容:

"hello\u0000world\u0000"

As Johnny Mopp pointed out, since you want to skip some characters, you need to use two index variables, and when you create the String at the end, you need to use that second index variable to limit how many characters you use to create the string.正如 Johnny Mopp 指出的那样,由于您想跳过某些字符,因此需要使用两个索引变量,并且在最后创建 String 时,您需要使用第二个索引变量来限制用于创建字符串的字符数字符串。

Since Java 9 you can use codePoints method:Java 9 开始,您可以使用codePoints方法:

public static void main(String[] args) {
    System.out.println(removeSpecialChars("hello world!")); // helloworld
    System.out.println(removeSpecialChars("^&*abc123_+"));  // abc
    System.out.println(removeSpecialChars("STRING"));       // STRING
    System.out.println(removeSpecialChars("Слово_Йй+ёЁ"));  // СловоЙйёЁ
}
public static String removeSpecialChars(String str) {
    return str.codePoints()
            // Stream<Character>
            .mapToObj(ch -> (char) ch)
            // filter out non-alphabetic characters
            .filter(Character::isAlphabetic)
            // Stream<String>
            .map(String::valueOf)
            // concatenate into a single string
            .collect(Collectors.joining());
}

See also: How do I count the parentheses in a string?另请参阅:如何计算字符串中的括号?

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

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