📜  Java中的 StringBuffer appendCodePoint() 方法及示例

📅  最后修改于: 2022-05-13 01:55:08.911000             🧑  作者: Mango

Java中的 StringBuffer appendCodePoint() 方法及示例

StringBuffer 类的 appendCodePoint() 方法将 codePoint 参数的字符串表示附加到此序列,我们需要先决条件了解 ASCII 表,因为只有这样我们才能感知输出为什么要附加特定字面量,因为已经存在分配了一个整数值。

--> appendCodePoint() Method
    --> StringBuffer Class
        --> java.lang Package 

句法:

public StringBuffer appendCodePoint(int cp)

参数:该方法接受整数类型的单个参数cp并引用 Unicode 代码点。

返回类型:该方法在附加由代码点表示的字符串后返回此对象。

插图:

Input : StringBuffer = Apple
        int cp = 65
Output: AppleA 
Input : StringBuffer = GeeksforGeeks
       int cp = 156
Output: GeeksforGeeks?

解释:因为 65 是 'A' 的 ASCII 值,而 156 是 '?' 的 ASCII 值

示例 1:

Java
// Java program to illustrate appendCodePoint() Method
// of StringBuffer class
 
// Importing required classes
import java.lang.*;
 
// Main class
public 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);
    }
}


Java
// Java Program to Illustrate appendCodePoint() Method
// of StringBuffer class
 
// Importing required classes
import java.lang.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) {
 
        // Reading passed string by creating objecgt 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);
    }
}


Java
// Java program to illustrate appendCodePoint() Method
// of StringBuffer class
 
// Importing required classes
import java.lang.*;
 
// Main class
public 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 = Geeksforgeeks
After appending CodePoint is = GeeksforgeeksA

示例 2:

Java

// Java Program to Illustrate appendCodePoint() Method
// of StringBuffer class
 
// Importing required classes
import java.lang.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String[] args) {
 
        // Reading passed string by creating objecgt 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 = Geeksforgeeks
After appending CodePoint is = Geeksforgeeks6

示例 3:

Java

// Java program to illustrate appendCodePoint() Method
// of StringBuffer class
 
// Importing required classes
import java.lang.*;
 
// Main class
public 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 = Geeksforgeeks
After appending CodePoint is = Geeksforgeeks+