我如何解析字符串值为字符类型,在Java?
我知道如何做到int和double(例如Integer.parseInt(“123”))。 有字串课吗?
我如何解析字符串值为字符类型,在Java?
我知道如何做到int和double(例如Integer.parseInt(“123”))。 有字串课吗?
当前回答
我发现这很有用:
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);
其他回答
如果你的字符串只包含一个字符,最简单的方法是调用charAt方法:
char c = s.charAt(0);
我发现这很有用:
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);
将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();
}
}
你可以简单地使用toCharArray()将字符串转换为char数组:
Scanner s=new Scanner(System.in);
System.out.print("Enter some String:");
String str=s.nextLine();
char a[]=str.toCharArray();
你可以在string中使用. charat (int)函数来检索任意索引处的char值。如果您想将String转换为char数组,请尝试在String上调用. tochararray()。如果字符串长度为1个字符,只需调用. charat(0)(或c#中的. first())来获取该字符。