String copy
In this chapter you will learn:
Copy a string
using System;/*j a v a 2s .c o m*/
class MainClass
{
public static void Main()
{
string myString4 = "string4";
Console.WriteLine( myString4);
string myString7 = String.Copy(myString4);
Console.WriteLine(myString7);
}
}
Check the equality for copied strings
using System; /*from j a v a 2s .c o m*/
class MainClass {
public static void Main() {
string str1 = "ABCDEabcde1234567890";
string str2 = string.Copy(str1);
string str3 = "asdf";
// compare strings
if(str1 == str2)
Console.WriteLine("str1 == str2");
else
Console.WriteLine("str1 != str2");
if(str1 == str3)
Console.WriteLine("str1 == str3");
else
Console.WriteLine("str1 != str3");
}
}
The code above generates the following result.
Copy sub string
using System;// j a v a 2s.c om
class Class1
{
[STAThread]
static void Main(string[] args)
{
string source = "ABCD";
char [] dest = { '1', '2', '3', '4', '5', '6', '7', '8' };
Console.Write( "Char array before = " );
Console.WriteLine( dest );
// copy substring into char array
source.CopyTo( 0, dest, 4, source.Length );
Console.Write( "Char array after = " );
Console.WriteLine( dest );
// copy back into source string
source = new String( dest );
Console.WriteLine( "New source = {0}\n", source );
}
}
Next chapter...
What you will learn in the next chapter:
Home » C# Tutorial » String