Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android如何获取字符串的第一个字符?_Java_String - Fatal编程技术网

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