从java中的String中删除插入的数字

时间:2012-12-12 02:48:07

标签: java

好的,所以我创建了一个程序来定义插入的单词是否是回文。 但我需要帮助删除在字符串中插入的数字。

import java.util.*;
import java.util.Scanner;
class Palindrome
{
   public static void main(String args[])
   {
      String reverse = "";
      Scanner scan = new Scanner(System.in);
      System.out.print("Type a sentence and press enter: ");
      String input = scan.nextLine();
      // use regex to remove the punctuation and spaces
      String Input = input.replaceAll("\\W", " ");
      System.out.println(Input);

      int length = input.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse.replaceAll("\\W", "") + input.charAt(i);   
      System.out.println(reverse);

      if (input.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
        System.out.println("Entered string is not a palindrome.");
   }
}

3 个答案:

答案 0 :(得分:6)

如果您想删除数字,请尝试input.replaceAll("[0-9]","")

答案 1 :(得分:0)

试试这个........

public class T1 {

    public static void main(String[] args){

        String s = "1234ajhdhols233adfjal"; 

        String[] arr = s.split("\\d");
        String sx = new String();
        for(String x : arr){

            sx = sx+x;
        }

        System.out.println(sx);
    }

}

答案 2 :(得分:0)

示例:

public static void main(String[] args) {
    String s = "1234ajhdhols233adfjal"; 
    String str = s.replaceAll("\\d", "");

    System.out.println(str);
}