copyvalueof_Java String copyValueOf()方法与示例

news/2024/7/19 14:17:19 标签: 字符串, java, python, 正则表达式, js

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


http://www.niftyadmin.cn/n/1255466.html

相关文章

Python学习 第二章 基础知识

要学习很牛B的编程技术之前我们还是得有点基础准备,哈哈,这是肯定的,那么我们现在就开始吧!!!1.1 变量 变量是撒子嘛?不好意思啊,我学习下四川话,哈哈,那么什…

c++知识细节-拷贝构造函数

c知识细节-拷贝构造函数 拷贝构造函数(复制构造函数) (1) 默认情况下,类对象的拷贝是每个成员变量逐个拷贝. (2) 如果一个类的构造函数的第一个参数是所属类类型的引用,如果还有其他参数,这些额外的参数还都有默认值,则这个构造函数就被称为拷贝构造函数. 作用: 会在一定的时…

isleap_Python日历模块| isleap()方法与示例

isleapPython calendar.isleap()方法 (Python calendar.isleap() Method) isleap() method is an inbuilt method of the calendar module in Python. It works on simple text calendars and checks whether the given year is a leap or not. It returns true if the year is…

c++知识细节-重载运算符/拷贝赋值运算符/析构函数

c知识细节-重载运算符/拷贝赋值运算符/析构函数 重载运算符 如何比较两个对象是否相等? 需要"重载 运算符". 说白了,我们要写一个成员函数,这个成员函数名为operator,这个成员函数里边,来写一些比较逻辑.例如: if(myTime1.Hour myTime2.Hour)return true;正式说…

c++知识细节-派生类/构造函数调用顺序/访问等级/函数遮蔽

c知识细节-派生类/调用顺序/访问等级/函数遮蔽 派生类 概念 (1) 类之间的层次关系,有父亲类,有孩子类. 例如车这个类,当做父类(也叫基类,超类),派生出卡车,轿车等孩子类(子类,派生类) 继承: 父子之间的这种层次关系焦作继承.是我们面向对象程序设计的核心. (2) 格式:class 子…

走格子问题

有编号1-n的n个格子,机器人从1号格子顺序向后走,一直走到n号格子,并需要从n号格子走出去。机器人有一个初始能量,每个格子对应一个整数Aii,表示这个格子的能量值。如果Aii > 0,机器人走到这个格子能够获…

c++知识细节-基类指针指向子类对象/虚纯虚函数/多态性/虚析构

c知识细节-基类指针指向子类对象/虚纯虚函数/多态性/虚析构 基类指针,派生类指针 新玩法: 父类指针可以new一个子类对象. Human *phuman new Men; phuman->funchuman(); //父类类型指针可以调用父类的成员函数 phuman->funcmen(); //不可以,因为你是一个父类指针Q: 既…

C++ 开源库列表

https://zh.cppreference.com/w/cpp/links/libs转载于:https://www.cnblogs.com/guozht/p/9347390.html