Подтвердить что ты не робот

Android Как получить первый символ строки?

Как получить первый символ строки?

string test = "StackOverflow";

первый символ = "S"

4b9b3361

Ответ 1

String test = "StackOverflow"; 
char first = test.charAt(0);

Ответ 2

Другой способ -

String test = "StackOverflow";
String s=test.substring(0,1);

В этом вы получили результат в String

Ответ 3

Как упоминалось всеми, вот полный фрагмент кода.

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

Перейдите к этой ссылке, прочитайте пункт 4.

Ответ 4

Использовать charAt():

public class Test {
   public static void main(String args[]) {
      String s = "Stackoverflow";
      char result = s.charAt(0);
      System.out.println(result);
   }
}

Вот tutorial