Java StringBuffer appendCodePoint()方法及实例
StringBuffer类的appendCodePoint()方法 将codePoint参数的字符串表示附加到这个序列中,为此我们需要有ASCII表的前提知识,因为只有这样我们才能感知输出,为什么特定的文字被附加,因为已经有一个整数值被分配。
--> appendCodePoint() Method --> StringBuffer Class --> java.lang Package
语法
public StringBuffer appendCodePoint(int cp)
参数: 该方法接受一个整数类型的单一参数 cp ,并指的是Unicode码位。
返回类型: 该方法在附加了代码点所代表的字符串后返回该对象。
示例
输入: StringBuffer = Apple
int cp = 65
输出: AppleA
输入: StringBuffer = GeeksforGeeks
int cp = 156
输出: GeeksforGeeks?
解释: 因为65是’A’的ASCII值,156是’?’的ASCII值。
例1 :
// Java program to illustrate appendCodePoint() Method// of StringBuffer class // Importing required classesimport java.lang.*; // Main classpublic class GFG { // Main drive method public static void main(String[] args) { // Reading passed string StringBuffer sbf = new StringBuffer("Geeksforgeeks"); // Printing the string System.out.println("String buffer = " + sbf); // Appending the CodePoint as String to the string // buffer sbf.appendCodePoint(65); // Printing the string again after // appending codePoint as string System.out.println("After appending CodePoint is = " + sbf); }}
输出
String buffer = GeeksforgeeksAfter appending CodePoint is = GeeksforgeeksA
例2 :
// Java Program to Illustrate appendCodePoint() Method// of StringBuffer class // Importing required classesimport java.lang.*; // Main classpublic class GFG { // Main driver method public static void main(String[] args) { // Reading passed string by creating object of class StringBuffer sbf = new StringBuffer("Geeksforgeeks"); System.out.println("String buffer = " + sbf); // Here it appends the CodePoint as // string to the string buffer sbf.appendCodePoint(54); System.out.println("After appending CodePoint is = " + sbf); }}输出
String buffer = GeeksforgeeksAfter appending CodePoint is = Geeksforgeeks6
例3 :
// Java program to illustrate appendCodePoint() Method// of StringBuffer class // Importing required classesimport java.lang.*; // Main classpublic class GFG { // Main driver method public static void main(String[] args) { // Reading passed string StringBuffer sbf = new StringBuffer("Geeksforgeeks"); // Printing string on console System.out.println("String buffer = " + sbf); // Appending the codePoint as string // to the string buffer sbf.appendCodePoint(43); // Printing the string on console after // appending codepoint as string System.out.println("After appending CodePoint is = " + sbf); }}输出
String buffer = GeeksforgeeksAfter appending CodePoint is = Geeksforgeeks+
