1

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

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.");
   }
}
4

3 回答 3

6

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

于 2012-12-12T02:51:25.047 回答
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);
    }

}
于 2012-12-12T03:18:10.950 回答
0

例子:

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

    System.out.println(str);
}
于 2015-01-12T23:38:32.263 回答