博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java StringBuilder getChars()方法与示例
阅读量:2530 次
发布时间:2019-05-11

本文共 3116 字,大约阅读时间需要 10 分钟。

StringBuilder类的getChars()方法 (StringBuilder Class getChars() method)

  • getChars() method is available in java.lang package.

    getChars()方法在java.lang包中可用。

  • getChars() method is used to copy all the characters from the given arguments (int src_st, int src_end) into another destination array of char type like char[] dest.

    getChars()方法用于将给定参数(int src_st,int src_end)中的所有字符复制到另一个char类型的目标数组中,例如char [] dest。

  • In this method copy first character starts at index src_st and copying the last character ends at index src_end so all the copied characters will be placed in an array char[] dest and this array index starts at dest_st and ends at dest_beg+(src_end-src_beg)-1.

    在此方法中,复制第一个字符开始于索引src_st ,复制最后一个字符结束于索引src_end,因此所有复制的字符都将放置在char [] dest数组中,并且此数组索引开始于dest_stg并结束于dest_beg +(src_end-sr​​c_beg) -1 。

  • getChars() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getChars()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • This method may throw an exception at the time of copying and placing copied characters.

    在复制和放置复制的字符时,此方法可能会引发异常。

    • IndexOutOfBoundsException – This exception may throw when src_st < 0 or dest_st < 0 or src_st > src_end or src_end > length().
    • IndexOutOfBoundsException-当src_st <0或dest_st <0或src_st> src_end或src_end> length()时,可能引发此异常。
    • NullPointerException – This exception may throw when char[] array is null exists.NullPointerException-如果char []数组为null,则可能引发此异常。

Syntax:

句法:

public void getChars(int src_st, int src_end, char[] dest, int dest_st);

Parameter(s):

参数:

  • int src_st – represents the index to start copying.

    int src_st –表示要开始复制的索引。

  • int src_end – represents the index to end copying.

    int src_end –表示结束复制的索引。

  • int char[] dest – represents the array for copied elements.

    int char [] dest –表示复制元素的数组。

  • int dest_beg – represents the index of starting position of char[] dest.

    int dest_beg –表示char [] dest的起始位置的索引。

Return value:

返回值:

The return type of this method is void, it returns nothing.

此方法的返回类型为void ,不返回任何内容。

Example:

例:

// Java program to demonstrate the example // of getChars(int src_st, int src_end, char[] dest, int dest_st)// method of StringBuilder public class GetChars {
public static void main(String[] args) {
int src_st = 0; int src_end = 4; int dest_st = 0; // Creating an StringBuilder object StringBuilder st_b = new StringBuilder("Java World"); // Display st_b System.out.println("st_b = " + st_b); char[] dest = new char[] {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j' }; // By using getChars() method is to copy the chars from the // given src_st to src_end of st_b and placed into dest[] // and position of placing the copied chars starts at dest_st st_b.getChars(src_st, src_end, dest, dest_st); // Display destination array for (char val: dest) System.out.print("" + val); }}

Output

输出量

st_b = Java WorldJavaefghij

翻译自:

转载地址:http://bixzd.baihongyu.com/

你可能感兴趣的文章
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>