我如何解析字符串值为字符类型,在Java?
我知道如何做到int和double(例如Integer.parseInt(“123”))。 有字串课吗?
我如何解析字符串值为字符类型,在Java?
我知道如何做到int和double(例如Integer.parseInt(“123”))。 有字串课吗?
当前回答
(un)EscapeJava方法可能是你想要的
来自聪明而不是我的回答:
https://stackoverflow.com/a/8736043/1130448
其他回答
将String转换为char的最简单方法是使用charAt():
String stringAns="hello";
char charAns=stringAns.charAt(0);//Gives You 'h'
char charAns=stringAns.charAt(1);//Gives You 'e'
char charAns=stringAns.charAt(2);//Gives You 'l'
char charAns=stringAns.charAt(3);//Gives You 'l'
char charAns=stringAns.charAt(4);//Gives You 'o'
char charAns=stringAns.charAt(5);//Gives You:: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
以下是完整的脚本:
import java.util.Scanner;
class demo {
String accNo,name,fatherName,motherName;
int age;
static double rate=0.25;
static double balance=1000;
Scanner scanString=new Scanner(System.in);
Scanner scanNum=new Scanner(System.in);
void input()
{
System.out.print("Account Number:");
accNo=scanString.nextLine();
System.out.print("Name:");
name=scanString.nextLine();
System.out.print("Father's Name:");
fatherName=scanString.nextLine();
System.out.print("Mother's Name:");
motherName=scanString.nextLine();
System.out.print("Age:");
age=scanNum.nextInt();
System.out.println();
}
void withdraw() {
System.out.print("How Much:");
double withdraw=scanNum.nextDouble();
balance=balance-withdraw;
if(balance<1000)
{
System.out.println("Invalid Data Entry\n Balance below Rs 1000 not allowed");
System.exit(0);
}
}
void deposit() {
System.out.print("How Much:");
double deposit=scanNum.nextDouble();
balance=balance+deposit;
}
void display() {
System.out.println("Your Balnce:Rs "+balance);
}
void oneYear() {
System.out.println("After one year:");
balance+=balance*rate*0.01;
}
public static void main(String args[]) {
demo d1=new demo();
d1.input();
d1.display();
while(true) {//Withdraw/Deposit
System.out.println("Withdraw/Deposit Press W/D:");
String reply1= ((d1.scanString.nextLine()).toLowerCase()).trim();
char reply=reply1.charAt(0);
if(reply=='w') {
d1.withdraw();
}
else if(reply=='d') {
d1.deposit();
}
else {
System.out.println("Invalid Entry");
}
//More Manipulation
System.out.println("Want More Manipulations: Y/N:");
String manipulation1= ((d1.scanString.nextLine()).toLowerCase()).trim();
char manipulation=manipulation1.charAt(0);
System.out.println(manipulation);
if(manipulation=='y') { }
else if(manipulation=='n') {
break;
}
else {
System.out.println("Invalid Entry");
break;
}
}
d1.oneYear();
d1.display();
}
}
我发现这很有用:
double --> Double.parseDouble(String);
float --> Float.parseFloat(String);
long --> Long.parseLong(String);
int --> Integer.parseInt(String);
char --> stringGoesHere.charAt(int position);
short --> Short.parseShort(String);
byte --> Byte.parseByte(String);
boolean --> Boolean.parseBoolean(String);
(un)EscapeJava方法可能是你想要的
来自聪明而不是我的回答:
https://stackoverflow.com/a/8736043/1130448
如果你想把一个String对象解析成一个char,而String对象代表了多个字符,你只需要简单地使用下面的表达式: char c = (char) Integer.parseInt(s)。 这里s等于你想要解析的字符串。大多数人忘记了char表示16位数字,因此可以是任何数值表达式的一部分:)
如果字符串长度为1个字符,就取该字符。如果字符串长度不超过1个字符,则不能将其解析为字符。