簡體   English   中英

從字符串中刪除空格、數字和特殊字符

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

我正在編寫一個函數來從作為參數傳遞的字符串中刪除空格。

此代碼有效:

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

以及字符串 'hello world!' 的輸出如下:

helloworld

但這個沒有:

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

輸出:

hello world

我得到相同的字符串作為輸出,但只刪除了感嘆號。 這可能是什么原因? 任何幫助,將不勝感激。

char值只是 0 到 2¹⁶−1 范圍內的數值。 在十六進制(基數 16)中,我們將其寫為 0000 到 ffff。

因此,知道每個char數組都是一個數值序列,讓我們在程序進行時查看每個數組的狀態。 (為簡潔起見,我將每個值顯示為兩個十六進制數字,而不是四個,因為它們都在 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 ]

我不知道你是如何檢查返回的字符串的,但這里是其中的實際內容:

"hello\u0000world\u0000"

正如 Johnny Mopp 指出的那樣,由於您想跳過某些字符,因此需要使用兩個索引變量,並且在最后創建 String 時,您需要使用第二個索引變量來限制用於創建字符串的字符數字符串。

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

另請參閱:如何計算字符串中的括號?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM