- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包补充
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包实用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java StringBuffer charAt() 方法
Java 的StringBuffer charAt() 方法用于获取此序列中指定索引处的字符值。由于 StringBuffer 与 String 相同(但可修改),因此其索引方式也类似。第一个字符值位于索引 0,下一个位于索引 1,以此类推,就像 String/数组索引一样。
注意 −
- 索引参数必须大于或等于 0,且小于此序列的长度。
- 如果索引指定的字符值是代理项,则返回代理项的值。
语法
以下是 Java StringBuffer charAt() 方法的语法
public char charAt(int index)
参数
- index − 这是所需字符值的索引。
返回值
此方法返回指定索引处的字符值。
示例:获取给定索引处的字符
当我们将索引作为参数传递给方法时,将返回该索引处的字符。
以下示例演示了 Java StringBuffer charAt() 方法的用法。在这里,我们声明一个 StringBuffer 对象并使用一系列字符对其进行初始化。该方法将索引作为参数来查找该索引处存在的字符。
package com.tutorialspoint; public class StringBufferDemo { public static void main(String[] args) { StringBuffer buff = new StringBuffer("Tutorials Point"); System.out.println("buffer = " + buff); // returns the char at index 4 System.out.println("character = " + buff.charAt(4)); buff = new StringBuffer("amrood admin "); System.out.println("buffer = " + buff); // returns the char at index 6, whitespace gets printed here System.out.println("character = " + buff.charAt(6)); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
buffer = Tutorials Point character = r buffer = amrood admin character =
示例:获取给定索引处的字符
另一个检索索引处存在的字符的示例如下所示。在这里,我们执行算术加法,并将结果作为参数传递给方法。
public class StringBufferCharAt { public static void main(String args[]) { StringBuffer sb = new StringBuffer("Tutorialspoint"); int id1 = 6; int id2 = 7; System.out.println("The character at " + (id1 + id2) + " index is: " + sb.charAt(id1 + id2)); } }
输出
让我们编译并运行上述程序,结果如下:
The character at 13 index is: t
示例:获取负索引处的字符时遇到异常
但是,当我们将负索引作为参数传递给方法时,会抛出 StringIndexOutOfBoundsException 异常。
public class StringBufferCharAt { public static void main(String args[]) { // Creating a StringBuffer object and Initializing it StringBuffer sb = new StringBuffer("Tutorialspoint"); // declare and assign the index value to an int variable int id = -4; // print the output System.out.println("The character at " + id + " index is: " + sb.charAt(id)); //throws exception } }
异常
如果我们尝试编译并运行上述程序,该方法将抛出 StringIndexOutOfBoundsException 异常:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index -4 out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4570) at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:358) at java.base/java.lang.StringBuffer.charAt(StringBuffer.java:243) at StringBufferCharAt.main(StringBufferCharAt.java:10)
示例:获取无效索引处的字符时遇到异常
在另一种情况下,如果我们传递的索引值大于输入序列的长度,该方法也会抛出 StringIndexOutOfBoundsException 异常。
public class StringBufferCharAt { public static void main(String args[]) { // Creating a StringBuffer object and Initializing it StringBuffer sb = new StringBuffer("Tutorialspoint"); // declare and assign the index value to an int variable int id = 15; // print the output System.out.println("The character at " + id + " index is: " + sb.charAt(id)); //throws exception } }
输出
如果我们尝试编译并运行上述程序,该方法将抛出 StringIndexOutOfBoundsException 异常:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302) at java.base/java.lang.String.checkIndex(String.java:4570) at java.base/java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:358) at java.base/java.lang.StringBuffer.charAt(StringBuffer.java:243) at StringBufferCharAt.main(StringBufferCharAt.java:13)
java_lang_stringbuffer.htm
广告