Java Android如何获取字符串的第一个字符?
如何获取字符串的第一个字符Java Android如何获取字符串的第一个字符?,java,string,Java,String,如何获取字符串的第一个字符 string test = "StackOverflow"; 第一个字符=“S”另一种方法是 String test = "StackOverflow"; String s=test.substring(0,1); 在这里,您得到了字符串使用charAt()的结果: 这里有一个你可以参考第4点 public class StrDemo { public static void main (String args[]) { String abc = "abc
string test = "StackOverflow";
第一个字符=“S”另一种方法是
String test = "StackOverflow";
String s=test.substring(0,1);
在这里,您得到了字符串使用charAt()的结果:
这里有一个你可以参考第4点
public class StrDemo
{
public static void main (String args[])
{
String abc = "abc";
System.out.println ("Char at offset 0 : " + abc.charAt(0) );
System.out.println ("Char at offset 1 : " + abc.charAt(1) );
System.out.println ("Char at offset 2 : " + abc.charAt(2) );
//Also substring method
System.out.println(abc.substring(1, 2));
//it will print
卑诗省
看看这个教程,我不认为这是离题的。我投票决定关闭,因为它是重复的。或者子字符串(0,1)
如果您希望它作为字符串而不是字符感谢mate,它是完美的。如果您执行textView.setText(test.charAt(0))
,它将抛出一个错误,因为它是字符而不是字符串。这就是@Thilo试图说的:`**String test=“StackOverflow”;字符串优先=测试子字符串(0,1)代码>**`
public class Test {
public static void main(String args[]) {
String s = "Stackoverflow";
char result = s.charAt(0);
System.out.println(result);
}
}
public class StrDemo
{
public static void main (String args[])
{
String abc = "abc";
System.out.println ("Char at offset 0 : " + abc.charAt(0) );
System.out.println ("Char at offset 1 : " + abc.charAt(1) );
System.out.println ("Char at offset 2 : " + abc.charAt(2) );
//Also substring method
System.out.println(abc.substring(1, 2));
//it will print
// as starting index to end index here in this case abc is the string
//at 0 index-a, 1-index-b, 2- index-c
// This line should throw a StringIndexOutOfBoundsException
System.out.println ("Char at offset 3 : " + abc.charAt(3) );
}
}