Q:

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

belongs to collection: C# Basic Programs | String programs

0

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.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Demonstrate the example of IndexOf() method of str... >>
<< Comparing two strings in C#...