我如何解析字符串值为字符类型,在Java?

我知道如何做到int和double(例如Integer.parseInt(“123”))。 有字串课吗?


当前回答

你可以使用这个技巧:

String s = "p";

char c = s.charAt(0);

其他回答

写作方式:

public class CharToInt{  
public static void main(String[] poo){  
String ss="toyota";
for(int i=0;i<ss.length();i++)
  {
     char c = ss.charAt(i); 
    // int a=c;  
     System.out.println(c); } } 
} 

关于输出,请参见此链接:单击这里

谢谢:-)

您可以执行以下操作:

String str = "abcd";
char arr[] = new char[len]; // len is the length of the array
arr = str.toCharArray();

我发现这很有用:

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);
import java.io.*;
class ss1 
{
    public static void main(String args[]) 
    {
        String a = new String("sample");
        System.out.println("Result: ");
        for(int i=0;i<a.length();i++)
        {
            System.out.println(a.charAt(i));
        }
    }
}

(un)EscapeJava方法可能是你想要的

来自聪明而不是我的回答:

https://stackoverflow.com/a/8736043/1130448