Java StringBuilder getChars()方法及示例

来源:这里教程网 时间:2026-02-17 21:14:12 作者:

Java StringBuilder getChars()方法及示例

StringBuilder类的 getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 方法 从StringBuilder包含的String中复制从给定的index:srcBegin到index:srcEnd-1的字符到一个作为参数传递给函数的char数组。**

这些字符从StringBuilder复制到数组dst[]中,从index:dstBegin开始,到index:dstbegin+(srcEnd-srcBegin)-1结束。第一个要从StringBuilder复制到数组的字符在索引 srcBegin处,最后一个要复制的字符在索引 srcEnd-1处。要复制的字符总数等于srcEnd-srcBegin。

语法

public void getChars(int srcBegin, int srcEnd,                           char[] dst, int dstBegin)

参数: 该方法接受四个不同的参数。

srcBegin: 代表我们必须开始复制的索引。srcEnd: 代表我们必须停止复制的索引。dst: 代表要复制数据到的数组。dstBegin: 代表我们开始粘贴复制的数据的目标数组的索引。

返回值: 该方法不返回任何东西。

异常: 该方法会抛出 StringIndexOutOfBoundsException ,如果。

srcBegin < 0dstBegin < 0srcBegin > srcEndsrcEnd > this.length()dstBegin+srcEnd-srcBegin > dst.length

以下程序演示了StringBuilder类的getChars()方法。

例1:

// Java program to demonstrate// the getChars() Method.  class GFG {    public static void main(String[] args)    {          // create a StringBuilder object        // with a String pass as parameter        StringBuilder            str            = new StringBuilder("WelcomeGeeks");          // print string        System.out.println("String = "                           + str.toString());          // create a char Array        char[] array = new char[7];          // get char from index 0 to 7        // and store in array start index 0        str.getChars(0, 7, array, 0);          // print char array after operation        System.out.print("Char array contains : ");          for (int i = 0; i < array.length; i++) {            System.out.print(array[i] + " ");        }    }}

输出:

String = WelcomeGeeksChar array contains : W e l c o m e

例2:

// Java program to demonstrate// the getChars() Method.  import java.util.*;  class GFG {    public static void main(String[] args)    {          // create a StringBuilder object        // with a String pass as parameter        StringBuilder            str            = new StringBuilder("evil dead_01");          // print string        System.out.println("String = "                           + str.toString());          // create a char Array        char[] array = new char[10];          // initialize all character to _(underscore).        Arrays.fill(array, '_');          // get char from index 5 to 9        // and store in array start index 3        str.getChars(5, 9, array, 3);          // print char array after operation        System.out.print("char array contains : ");          for (int i = 0; i < array.length; i++) {            System.out.print(array[i] + " ");        }    }}

输出:

String = evil dead_01char array contains : _ _ _ d e a d _ _ _

例3:演示StringIndexOutOfBoundException

// Java program to demonstrate// exception thrown by the getChars() Method.  import java.util.*;  class GFG {    public static void main(String[] args)    {          // create a StringBuilder object        // with a String pass as parameter        StringBuilder            str            = new StringBuilder("evil dead_01");          // create a char Array        char[] array = new char[10];          // initialize all character to _(underscore).        Arrays.fill(array, '_');          try {              // if start is greater then end            str.getChars(5, 2, array, 0);        }          catch (Exception e) {            System.out.println("Exception: " + e);        }    }}

输出:

Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd

参考资料:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#getChars(int, int, char%5B%5D, int)。

相关推荐