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

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


当前回答

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));
        }
    }
}

其他回答

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));
        }
    }
}

如果字符串长度为1个字符,就取该字符。如果字符串长度不超过1个字符,则不能将其解析为字符。

我发现这很有用:

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中使用. charat (int)函数来检索任意索引处的char值。如果您想将String转换为char数组,请尝试在String上调用. tochararray()。如果字符串长度为1个字符,只需调用. charat(0)(或c#中的. first())来获取该字符。

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

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

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