- 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 appendCodePoint() 方法
Java 的StringBuffer appendCodePoint() 方法将代码点的字符串表示形式附加到一个序列中。简单来说,此方法接受的参数与序列的内容组合起来形成一个新的字符串。此序列的长度将增加 Character.charCount(codePoint) 方法的结果。
此方法的整体效果与使用 Character.toChars(int) 方法将参数转换为 char 数组,然后将该数组中的字符附加到此字符序列完全相同。
语法
以下是 Java StringBuffer appendCodePoint() 方法的语法
public StringBuffer appendCodePoint(int codePoint)
参数
- codePoint − 一个 Unicode 代码点
返回值
此方法返回对该 StringBuffer 对象的引用。
示例:将代码点的字符串表示形式附加到 StringBuffer
在下面的示例中,当我们简单地将 Unicode 代码点作为参数传递给此方法时,此代码点的字符将附加到输入序列并返回。
public class StringBufferAppend_cdp { public static void main(String args[]) { StringBuffer word = new StringBuffer("Tutorialspoin"); int cp = 116; System.out.println("The input sequence: " + word); System.out.println("The unicode code point: " + cp); System.out.println("The appended sequence: " + word.appendCodePoint(cp)); } }
输出
编译并执行给定的程序后,输出将显示如下:
The input sequence: Tutorialspoin The unicode code point: 116 The appended sequence: Tutorialspoint
示例:将字符强制转换为代码点后附加到 StringBuffer
我们也可以通过类型转换将字符值作为参数传递给此方法,并且该方法仍然会返回附加的序列。
public class StringBufferAppend_cdp { public static void main(String args[]) { StringBuffer word = new StringBuffer("Tutorialspoint"); char ch = '!'; System.out.println("The input sequence: " + word); System.out.println("The character to be appended: " + ch); System.out.println("The appended sequence: " + word.appendCodePoint((int)ch)); } }
输出
让我们编译并运行上面的程序,以生成如下所示的输出:
The input sequence: Tutorialspoint The character to be appended: ! The appended sequence: Tutorialspoint!
示例:使用 Null 时遇到编译错误
但是,如果我们将 null 值作为参数传递给此方法,则该方法将抛出编译时错误。
public class StringBufferAppend_cdp { public static void main(String args[]) { StringBuffer word = new StringBuffer("Tutorialspoint"); int ch = null; // a compile time error as there is no code point for null values System.out.println("The appended sequence: " + word.appendCodePoint(ch)); } }
编译时错误
如果我们尝试编译并运行上面的程序,则会引发如下所示的编译时错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from null to char at StringBufferAppend_cdp.main(StringBufferAppend_cdp.java:7)
示例:使用无效代码点时遇到异常
假设我们将 Unicode 系统中不存在的代码点作为参数传递给该方法,则该方法将抛出 IllegalArgumentException。
public class StringBufferAppend_cdp { public static void main(String args[]) { StringBuffer word = new StringBuffer("Tutorialspoint"); int cp = -123; // throws an exception System.out.println("The appended sequence: " + word.appendCodePoint(cp)); } }
异常
如果我们尝试编译并运行上面的程序,则会抛出 IllegalArgumentException,因为代码点不能为负:
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFF85 at java.base/java.lang.Character.toChars(Character.java:9493) at java.base/java.lang.AbstractStringBuilder.appendCodePoint(AbstractStringBuilder.java:962) at java.base/java.lang.StringBuffer.appendCodePoint(StringBuffer.java:443) at StringBufferAppend_cdp.main(StringBufferAppend_cdp.java:10)
java_lang_stringbuffer.htm
广告