java charat
时间: 2023-12-19 15:52:57 浏览: 83
在java中如何使用charAt方法
The charAt() method is a method in the Java programming language that is used to return the character at a specified index in a string. It is a built-in method of the String class and is used to extract a single character from a string.
Here is an example of how to use the charAt() method in Java:
```
String str = "Hello World";
char c = str.charAt(0);
System.out.println(c);
```
This code will output the character at the first index of the string "Hello World", which is 'H'.
The charAt() method takes an integer parameter that specifies the index of the character to be returned. The index starts at 0 for the first character in the string, and goes up to the length of the string minus 1 for the last character. If the specified index is greater than or equal to the length of the string, a StringIndexOutOfBoundsException will be thrown.
阅读全文