📌  相关文章
📜  在Java从字符串中删除所有数字的不同方法

📅  最后修改于: 2021-05-17 18:01:31             🧑  作者: Mango

给定字母数字字符串str,任务是编写一个Java程序以删除该字符串中的所有数字并打印修改后的字符串。

例子:

方法1:使用String.toCharArray()方法

  1. 获取字符串以删除所有数字。
  2. 将给定的字符串转换为字符数组。
  3. 初始化一个空字符串,存储结果。
  4. 从头到尾遍历字符数组。
  5. 检查指定字符是否不是数字,然后将此字符添加到结果变量中。
  6. 现在,打印结果。

下面是上述方法的实现:

Java
// Java program to remove all the
// digit from string
class GFG {
  
    // Function to remove all the digit
    // from string
    public static String removeAllDigit(String str)
    {
        // Converting the given string
        // into a character array
        char[] charArray = str.toCharArray();
        String result = "";
  
        // Traverse the character array
        for (int i = 0; i < charArray.length; i++) {
  
            // Check if the specified character is not digit
            // then add this character into result variable
            if (!Character.isDigit(charArray[i])) {
                result = result + charArray[i];
            }
        }
  
        // Return result
        return result;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given alphanumeric string str
        String str = "GeeksForGeeks123";
  
        // Print the modified string
        System.out.println(removeAllDigit(str));
    }
}


Java
// Java program to remove all the
// digit from string
class GFG {
  
    // Function to remove all the digit
    // from string
    public static String removeAllDigit(String str)
    {
        String result = "";
  
        // Traverse the String from start to end
        for (int i = 0; i < str.length(); i++) {
  
            // Check if the specified character is not digit
            // then add this character into result variable
            if (!Character.isDigit(str.charAt(i))) {
                result = result + str.charAt(i);
            }
        }
  
        // Return result
        return result;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given alphanumeric string str
        String str = "GeeksForGeeks123";
  
        // Print the modified string
        System.out.println(removeAllDigit(str));
    }
}


Java
// Java program to remove all the
// digit from string
class GFG {
  
    // Function to remove all the digit
    // from string
    public static String removeAllDigit(String str)
    {
        // Replaces all the sequence of characters
        // that matches the given regex with
        // the given replacement string
        return str.replaceAll("\\d", "");
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given alphanumeric string str
        String str = "GeeksForGeeks123";
  
        // Print the modified string
        System.out.println(removeAllDigit(str));
    }
}


输出
GeeksForGeeks

  • 时间复杂度: O(N)
  • 空间复杂度: O(N)

方法2:使用String.charAt()方法

  1. 获取字符串以删除所有数字。
  2. 初始化一个空字符串,存储结果。
  3. 从头到尾遍历字符串。
  4. 检查指定字符是否不是数字,然后将此字符添加到结果变量中。
  5. 现在,打印结果。

下面是上述方法的实现:

Java

// Java program to remove all the
// digit from string
class GFG {
  
    // Function to remove all the digit
    // from string
    public static String removeAllDigit(String str)
    {
        String result = "";
  
        // Traverse the String from start to end
        for (int i = 0; i < str.length(); i++) {
  
            // Check if the specified character is not digit
            // then add this character into result variable
            if (!Character.isDigit(str.charAt(i))) {
                result = result + str.charAt(i);
            }
        }
  
        // Return result
        return result;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given alphanumeric string str
        String str = "GeeksForGeeks123";
  
        // Print the modified string
        System.out.println(removeAllDigit(str));
    }
}
输出
GeeksForGeeks

  • 时间复杂度: O(N)
  • 空间复杂度: O(1)

方法3:使用String.replaceAll()方法

这个想法是使用String.replaceAll()方法,用给定的替换字符串替换与给定的正则表达式匹配的所有字符序列。

下面是上述方法的实现:

Java

// Java program to remove all the
// digit from string
class GFG {
  
    // Function to remove all the digit
    // from string
    public static String removeAllDigit(String str)
    {
        // Replaces all the sequence of characters
        // that matches the given regex with
        // the given replacement string
        return str.replaceAll("\\d", "");
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // Given alphanumeric string str
        String str = "GeeksForGeeks123";
  
        // Print the modified string
        System.out.println(removeAllDigit(str));
    }
}
输出
GeeksForGeeks

  • 时间复杂度: O(N)
  • 空间复杂度: O(1)