How to copy a string in C#

8 Answers

0 votes
// Copy using simple assignment

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        
        string dest = src; // Copies reference only

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
edited 20 hours ago by avibootz
0 votes
// Copy using string.Copy()

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        
        string dest = string.Copy(src);

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using new string(char[])

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";

        string dest = new string(src.ToCharArray());

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using string.Format()

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";

        string dest = string.Format("{0}", src);

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using StringBuilder

using System.Text;

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        StringBuilder sb = new StringBuilder();

        sb.Append(src);
        string dest = sb.ToString();

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using ToCharArray()

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        
        char[] arr = src.ToCharArray();
        string dest = new string(arr);

        System.Console.WriteLine(dest);
    }
}




/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using Array.Copy()

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        
        char[] srcArr = src.ToCharArray();
        char[] destArr = new char[srcArr.Length];

        System.Array.Copy(srcArr, destArr, srcArr.Length);
        string dest = new string(destArr);

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 1 day ago by avibootz
0 votes
// Copy using manual loop

class CopyString
{
    static void Main()
    {
        string src = "Programming is fun";
        char[] destArr = new char[src.Length];

        for (int i = 0; i < src.Length; i++)
            destArr[i] = src[i];

        string dest = new string(destArr);

        System.Console.WriteLine(dest);
    }
}



/*
run:

Programming is fun

*/

 



answered 20 hours ago by avibootz

Related questions

6 answers 13 views
7 answers 12 views
8 answers 24 views
8 answers 20 views
7 answers 16 views
10 answers 20 views
...