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


namespace StringExample
{
class Program
{
public static void DemoConstruct()
{
// 1. The string type represents a sequence of zero or more
// Unicode characters. string is an alias for String in
// the .NET Framework.
// 2. if you need to create a instance of string,
// you don't need to use "new:
// just use it as value type though it's reference type

string str1 = "abc",
str2 = "aaa";
Console.WriteLine("str1 = " + str1);
Console.WriteLine("str2 = " + str2);

str1 = str2;
Console.WriteLine("str1 = " + str1);
}

public static void DemoClone()
{
string str1 = "abb";
string str2 = "";
Console.WriteLine("before call Clone ...");
Console.WriteLine("str1 = " + str1);
Console.WriteLine("str2 = " + str2);

// clone: Returns a reference to this instance of String.
str2 = (string)str1.Clone();

// check whether these two objects are the same
Console.WriteLine( "str1 == str2 : " + Object.ReferenceEquals(str1, str2));

// after assigned to "aaa", str1 points to new reference
str1 = "aaa";
Console.WriteLine("after call Clone ...");
Console.WriteLine("str1 = " + str1); // str1 = "aaa"
Console.WriteLine("str2 = " + str2); // str2 = "abb"

}

public static void DemoCompareTo()
{
string str1 = "aaaa";
string str2 = "bbbb";
string str3 = "cccc";
string str4 = "aaaa";

int result = str1.CompareTo(str2);
Console.WriteLine("compare str1, str2 = " + result);

result = str3.CompareTo(str1);
Console.WriteLine("compare str3, str1 = " + result);

result = str1.CompareTo(str4);
Console.WriteLine("compare str1, str4 = " + result);
}

static void Main(string[] args)
{
DemoConstruct();
DemoClone();
DemoCompareTo();

// output
// str1 = abc
// str2 = aaa
// str1 = aaa
// before call Clone ...
// str1 = abb
// str2 =
// str1 == str2 : True
// after call Clone ...
// str1 = aaa
// str2 = abb
// compare str1, str2 = -1
// compare str3, str1 = 1
// compare str1, str4 = 0

}
}
}
arrow
arrow
    全站熱搜

    kojenchieh 發表在 痞客邦 留言(1) 人氣()