copyvalueof_Java String copyValueOf()方法与示例

copyvalueof

字符串copyValueOf()方法 (String copyValueOf() Method)

copyValueOf() is a String method in Java and it is used to create a string with given character array i.e. it accepts a character array and returns a string. In other words, we can say copyValueOf() method is used to copy the value of character array to the string.

copyValueOf()是Java中的String方法,用于创建具有给定字符数组的字符串,即,它接受一个字符数组并返回一个字符串。 换句话说,可以说copyValueOf()方法用于将字符数组的值复制到字符串。

Syntax:

句法:

    String str_object.concat(char[] chr, [int offset], [int len]);

Here,

这里,

  • chr is a character array, we have to convert into a string.

    chr是一个字符数组,我们必须转换为字符串。

  • str_object is the main string in which we have to copy the copy the value of the character array.

    str_object是主字符串,我们必须在其中复制副本字符数组的值。

  • offset is an optional parameter, it is used to set the starting offset (position), from where we want to copy the character array as a string.

    offset是一个可选参数,用于设置起始偏移量(位置),从该位置我们要将字符数组复制为字符串。

  • length is also an optional parameter, it is used to define the number of characters to be copied as a string.

    length也是一个可选参数,它用于定义要复制为字符串的字符数。

Method accepts a character array and returns string.

方法接受一个字符数组并返回字符串。

Example:

例:

    Input: 
    char char_arr[] = {'I','n','c','l','u','d','e','h','e','l','p'};

    Function call                       Output
    str.copyValueOf(char_arr)           Includehelp
    str.copyValueOf(char_arr, 7, 4)     help

Java code to demonstrate the example of String.copyValueOf() method

Java代码演示String.copyValueOf()方法的示例

public class Main
{
    public static void main(String[] args) {
        char char_arr[] = {'I','n','c','l','u','d','e','h','e','l','p'};
        String str ="";
        
        //copying char[] and creating string 
        str = str.copyValueOf(char_arr);
        System.out.println("str = " + str);
        
        //using offset and length
        str = str.copyValueOf(char_arr, 7, 4);
        System.out.println("str = " + str);

    }
}

Output

输出量

str = Includehelp
str = help


翻译自: https://www.includehelp.com/java/string-copyValueOf-method-with-example.aspx

copyvalueof

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值