[英]How to ignore special characters and spaces in string?
因此,我开发了此代码将单词转换为电话号码,并且在显示结果时如何编码它以忽略输入的空格?
所以我的意思是允许用户在单词之间输入空格,但不反映在结果中。
import java.util.Scanner;
{
public static void main (String[] args)
{
Scanner console = new Scanner(System.in);
{
System.out.println("Enter the a word to be converted : ");
String Letter = console.next ();
Letter = Letter.toUpperCase();
Letter = Letter.toLowerCase();
String Number="";
int count=0;
int i=0;
while(count < Letter.length())
{switch(Letter.charAt(i))
{case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
Number += "2";
count++;
break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
Number += "3";
count++;
break;
case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
Number += "4";
count++;
break;
case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':
Number += "5";
count++;
break;
case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
Number += "6";
count++;
break;
case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
Number += "7";
count++;
break;
case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
Number += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
Number += "9";
count++;
break;
}
if( count==3) {
Number += "-";
}
i++;
}
System.out.println( Number );
}
}}
要忽略空格,可以使用以下命令:
String.trim();
这将trim
字符串中的所有空格。 有关更多信息,请参见String.trim() 。
并检查字符串是否除字母外还包含其他内容,您可以使用:
public boolean isAlpha(String name) {
char[] chars = name.toCharArray();
for (char c : chars) {
if(!Character.isLetter(c)) {
return false;
}
}
return true;
}
如果需要速度,或者为了简单起见,可以使用:
public boolean isAlpha(String name) {
return name.matches("[a-zA-Z]+");
}
String content = "asda saf oiadgod iodboiosb dsoibnos";
content = content.replaceAll("\\s", "");
System.out.println(content);
为您的代码
System.out.println("Enter the a word to be converted : ");
String Letter = console.nextLine();
Letter = Letter.replaceAll("\\s", "");
Letter = Letter.toUpperCase();
Letter = Letter.toLowerCase();
String Number = "";
import java.util.Scanner;
{
public static void main (String[] args)
{
Scanner console = new Scanner(System.in);
{
System.out.println("Enter the a word to be converted : ");
String Letter = console.next ();
Letter = Letter.toUpperCase();
Letter = Letter.toLowerCase();
String Number="";
int count=0;
int i=0;
while(count < Letter.length())
{switch(Letter.charAt(i))
{
case 'A':case 'B':case 'C': case 'a': case 'b': case 'c':
Number += "2";
count++;
break;
case 'D':case 'E':case 'F': case 'd': case 'e': case 'f':
Number += "3";
count++;
break;
case 'G':case 'H':case 'I': case 'g': case 'h': case 'i':
Number += "4";
count++;
break;
case 'J':case 'K':case 'L': case 'j': case 'k': case 'l':
Number += "5";
count++;
break;
case 'M':case 'N':case 'O': case 'm': case 'n': case 'o':
Number += "6";
count++;
break;
case 'P':case 'R':case 'S': case 'p': case 'r': case 's':
Number += "7";
count++;
break;
case 'T':case 'U':case 'V': case 't': case 'u': case 'v':
Number += "8";
count++;
break;
case 'W':case 'X':case 'Y':case 'Z': case 'w': case 'x': case 'y': case 'z':
Number += "9";
count++;
break;
default:
//Ignore anything else
break;
}
if( count==3) {
Number += "-";
}
i++;
}
System.out.println( Number );
}
}}
通过在交换机的情况下使用default,您可以忽略所有其他响应。因此,如果y键入交换机中未包含的任何内容,则不会添加到您的计数或数字中。
您可以将非字母数字字符串中的字符替换为blank( ""
),然后使用该字符串进行处理。 您可以使用String.replaceAll()方法。
用给定的替换项替换该字符串中与给定的正则表达式匹配的每个子字符串。
例如:
String str = "abc..,df.,";
String alphaNumericStr = str.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(alphaNumericStr); // Prints > abcdf
while(count < alphaNumericStr.length()) { // using alphaNumericStr instead of Letter
...
另一种方法 (我希望这样):@KeshavPandey的答案
如果要模拟数字小键盘,则可能应该使用blank space
并将字符串附加0
。
大多数手机的数字0
键上都有blank space
。
case ' ':
Number += "0";
count++;
break;
以下代码段可能会对您有所帮助。 我刚刚在上面优化了您的代码。 您可以使用String API用数字替换字符,而不是逐个字符地迭代字符串字符并生成数字。
Scanner console = new Scanner(System.in);
String str = console.next();
// To trim the leading and trailing white spaces
str = str.trim();
// To remove the white spaces in between the string
while (str.contains(" ")) {
str = str.replaceAll(" ", "");
}
// To replace the letters with numbers
str = str.replaceAll("[a-cA-C]", "2").replaceAll("[d-fD-F]", "3")
.replaceAll("[g-iG-I]", "4").replaceAll("[j-lJ-L]", "5")
.replaceAll("[m-oM-O]", "6").replaceAll("[p-sP-S]", "7")
.replaceAll("[t-vT-V]", "8").replaceAll("[w-zW-Z]", "9");
System.out.println(str);
如果要在3位数字后插入“-”,则可以在上述转换后使用以下片段代码。
StringBuffer buff = new StringBuffer(str);
buff.insert(3, "-");
System.out.println(buff.toString());
您可以仅添加一条额外的case语句来检查要避免的字符。 然后,当此操作被击中时,“什么也不做”
在只想跳过空格的情况下,可以添加特定于''字符的其他大小写和/或默认大小写;
case ' ':
default:
break;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.