回答
String test = "StackOverflow";
char first = test.charAt(0);
或'substring(0,1)'如果你想要它作为一个字符串而不是一个字符 – Thilo 2014-09-24 07:06:56
感谢队友,它的完美 – user1710911 2014-09-24 07:10:28
这将抛出一个错误,如果你做'textView.setText(test.charAt(0))'作为它是一个字符而不是字符串。 – Prabs 2017-04-28 08:15:40
另一种方式是
String test = "StackOverflow";
String s=test.substring(0,1);
在此你有导致String
使用的charAt():
public class Test {
public static void main(String args[]) {
String s = "Stackoverflow";
char result = s.charAt(0);
System.out.println(result);
}
}
这是一个tutorial
正如大家所说,这里是完整的代码片段。
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));
}
}
回到这个link,读取点4
- 1. 如何获取SQL中的字符串的第一个字符?
- 2. Ruby:如何获取字符串的第一个字符
- 3. 如何获取字符串的第一个字符
- 4. 获取字符串中子字符串的第一个字母
- 5. C#:如何获得一个字符串的第一个字符?
- 6. 获取第二个字符/字符串出现的字符串
- 7. 获取每个单词的第一个字符的字符串
- 8. 如何获取函数来遍历字符串并替换字符串的第一个字符,第二个等?
- 9. 从字符串数组中获取第一个字符串c
- 10. 如何获取字符串中第一个字母的位置
- 11. 如何在Android中替换字符串的第一个字符
- 12. 的Java获取第一个字符值的字符串
- 13. 树枝:如何获得字符串中的第一个字符
- 14. 获取UTF-8字符串的第一个字符
- 15. 获取并删除字符串的第一个字符
- 16. 获取字符串中第一个字符的Unicode值
- 17. 在java中的字符串操作,获取第一个字符
- 18. Python:获取列表中第一个字符串的第一个字符?
- 19. 如何从字符串中获取第一个Y后的X个字符
- 20. 如何获得的第一个字一个字符串在Excel
- 21. 获取字符串中第一个出现的多个字符串之一
- 22. 如何获取字符串中的第三个和第四个分隔字符之间的子字符串?
- 23. 如何从另一个字符串获取字符串值?
- 24. 如何从另一个字符串获取字符串值
- 25. string [0]从字符串中获取第一个字符
- 26. 获取第一个空格字符和'='字符之间的字符串
- 27. 如何从字符串中获取第2个和第4个字符
- 28. 如何从字符串中提取字符串的第一个数字
- 29. 获取字符串的第一个字,并从实际字符串
- 30. 如何通过Jquery或javaScript获取字符串的第一个字符?
看看这个教程[的charAt()](HTTP://www.tutorialspoint .com/java/java_string_charat.htm) – 2014-09-24 07:06:52
可能重复[获取字符串字符的索引-Java](http://stackoverflow.com/questions/11229986/get-string-character-by-index-java) – 2014-09-24 07:09:42
我不要认为这是无关紧要的。由于它是重复的,我投票结束。 – Keppil 2014-09-24 07:23:09