我如何解析字符串值为字符类型,在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));
        }
    }
}

其他回答

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

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

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

你可以使用这个技巧:

String s = "p";

char c = s.charAt(0);

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

您可以执行以下操作:

String str = "abcd";
char arr[] = new char[len]; // len is the length of the array
arr = str.toCharArray();
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));
        }
    }
}