java 如何从字符串中分离字母和符号。(几乎完成)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15116705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Separate Letters and Symbols from String.(Almost Done)
提问by Mr.Yoso
import java.util.Scanner;
public class Separate {
public static void main(String[] args) {
Scanner user_input = new Scanner( System.in );
String variable;
System.out.print("Enter Variable:");
variable = user_input.next();
Separate(variable);
}
public static void Separate(String str) {
String number = "";
String letter = "";
String symbol = "";
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (Character.isDigit(a)) {
number = number + a;
} else {
letter = letter + a;
}
}
System.out.println("Alphabets in string:"+letter);
System.out.println("Numbers in String:"+number);
}
}
}
Okay, i already have this code which separate the Numbers and Letters that i Input. The problem is, when ever i input Symbols for example (^,+,-,%,*) it also states as a Letter.
好的,我已经有了将我输入的数字和字母分开的代码。问题是,当我输入符号时,例如 (^,+,-,%,*) 它也表示为字母。
What i want to do is to separate the symbol from letters just like what i did on Numbers and Letters and make another output for it.
我想要做的是将符号与字母分开,就像我在数字和字母上所做的那样,并为其制作另一个输出。
回答by Sarath Kumar Sivan
public static void separate(String string) {
StringBuilder alphabetsBuilder = new StringBuilder();
StringBuilder numbersBuilder = new StringBuilder();
StringBuilder symbolsBuilder = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
char ch = string.charAt(i);
if (Character.isAlphabetic(ch)) {
alphabetsBuilder.append(ch);
} else if (Character.isDigit(ch)) {
numbersBuilder.append(ch);
} else {
symbolsBuilder.append(ch);
}
}
System.out.println("Alphabets in string: " + alphabetsBuilder.toString());
System.out.println("Numbers in String: " + numbersBuilder.toString());
System.out.println("Sysmbols in String: " + symbolsBuilder.toString());
}
回答by CosminO
You are testing if the character isDigit
, else
treat it as a letter.
Well, if it is not a digit, all other cases go to else
, in your code. Create an else
case for those symbols also.
您正在测试如果字符isDigit
,else
把它当作一个字母。好吧,如果它不是数字,则所有其他情况都转到else
, 在您的代码中。else
也为这些符号创建一个案例。
回答by Joop Eggen
As this reeks of homework, just see the documentation of Character, which had that nice function isDigit
.
回答by user93353
public static void Separate(String str)
{
String number = "";
String letter = "";
String symbol = "";
for (int i = 0; i < str.length(); i++)
{
char a = str.charAt(i);
if (Character.isDigit(a))
{
number = number + a;
continue;
}
if(Character.isLetter(a))
{
letter = letter + a;
}
else
{
symbol = symbol + a;
}
}
System.out.println("Alphabets in string:"+letter);
System.out.println("Numbers in String:"+number);
}
回答by Pranav
import java.util.Scanner;
public class Separate {
public static void main(String[] args) {
Scanner user_input = new Scanner( System.in );
String variable;
System.out.print("Enter Variable:");
variable = user_input.next();
Separate(variable);
}
public static void Separate(String str)
{
String number = "";
String letter = "";
String symbol = "";
for (int i = 0; i < str.length(); i++) {
char a = str.charAt(i);
if (Character.isDigit(a)) {
number = number + a;
} else if (Character.isLetter(a)) {
letter = letter + a;
} else {
symbol = symbol + a;
}
}
System.out.println("Alphabets in string:"+letter);
System.out.println("Numbers in String:"+number);
System.out.println("Symbols in String:"+symbol);
}
}