索引从0开始,所以第一个字符的索引是0,第二个字符的索引是1,以此类推。 示例代码 下面是一个示例代码,演示如何使用charAt()方法输出字符串中的指定位置字符: publicclassMain{publicstaticvoidmain(String[]args){Stringstr="Hello, World!";charch=str.charAt(7);System.ou
Stringstr="Hello, World!";charch=str.charAt(7); System.out.println("Character at index 7: "+ ch); 在上面的例子中,字符串str的索引从0开始,所以字符’H’的索引是0,字符’,'的索引是5,字符’W’的索引是7。因此,上面的代码将输出: Character at index 7:W...
该方法接收一个整数参数,代表要返回的字符的索引位置,然后返回该位置上的字符。 让我们来看一个简单的示例,演示如何使用charAt()方法返回字符串中指定位置的字符: publicclassMain{publicstaticvoidmain(String[]args){Stringstr="Hello, World!";charch=str.charAt(7);System.out.println("Character at index 7:...
".Stringstr="Java Exercises!";// Print the original string.System.out.println("Original String = "+str);// Get the character at positions 0 and 10.intindex1=str.charAt(0);// Get the ASCII value of the character at position 0.intindex2=str.charAt(10);// Get the ASCII value of t...
public static void main(String[] args) { // TODO Auto-generated method stub String str = "This is yiibai"; // prints character at 1st location System.out.println(str.charAt(0)); // prints character at 5th location i.e white-space character System.out.println(str.charAt(4)); // pri...
or you can use a StringBuilder: StringBuilder myName =newStringBuilder("domanokz"); myName.setCharAt(4, 'x'); System.out.println(myName); http://stackoverflow.com/questions/6952363/java-replace-a-character-at-a-specific-index-in-a-string...
public static void main(String[] args) { // TODO Auto-generated method stub String str = "This is yiibai"; // prints character at 1st location System.out.println(str.charAt(0)); // prints character at 5th location i.e white-space character ...
Character类中的另外一个toString方法。 1toString2publicstaticString toString(charc) 返回一个表示指定char值的 String 对象。结果是长度为 1 的字符串,仅由指定的char组成。34参数:5c - 要转换的char值6返回:7指定char值的字符串表示形式8从以下版本开始:91.4 ...
StringCharacterIterator(String, Int32, Int32, Int32) Constructs an iterator over the given range of the given string, with the index set at the specified position. StringCharacterIterator(String, Int32) Constructs an iterator with the specified initial index. StringCharacterIterator(String) Constr...
endIndex(optional) - the ending index substring() Return Value Thesubstring()method returns a substring from the given string. The substring begins with the character at thestartIndexand extends to the character at indexendIndex - 1 If theendIndexis not passed, the substring begins with the ...