Java Android 如何获取字符串的第一个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26010602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:41:18  来源:igfitidea点击:

Android How to get first character of string?

javastring

提问by user1710911

How to get first character of string?

如何获取字符串的第一个字符?

string test = "StackOverflow";

first character = "S"

第一个字符 = "S"

采纳答案by Karakuri

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

回答by M D

Another way is

另一种方式是

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

In this you got result in String

在这个你得到了结果 String

回答by Sagar Pilkhwal

Use charAt():

使用 charAt():

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

Here is a tutorial

这是一个教程

回答by ajitksharma

You can refer this link, point 4.

您可以参考此链接,第 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 

bc

公元前

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