回答
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