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