Home » .Net

Demonstrate the example of Copy method of String class in C#

String.Copy() method in C#: Here, we will learn with an example, how to create an instance of existing string? Copy method creates instance of the string and copies existing value to new object.

Given a string object and we have to create another instance of this object using String.Copy() method in C#.

String.Copy() Method

It is a method of String class which is used to create a new instance of String class copy value of already created String.

Syntax:

String String.Copy(String str);

Above method return newly created instance with value of passed string.

Example of String.Copy() in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            String str1;
            String str2;

            Console.Write("Enter string : ");
            str1 = Console.ReadLine();
            str2 = String.Copy(str1);

            
            Console.WriteLine("Value is str1 : " + str1);
            Console.WriteLine("Value of str2 : " + str2);
        }
    }
}

Output

Enter string : Hello how are you
Value is str1 : Hello how are you
Value of str2 : Hello how are you

Here, we are declare a string object str1 and assigning the string value by reading from console, then we created another object of string str2 using String.Copy(), where str2 is the instance of str1 with the same value assigned in str1.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.