字符串string
String类的属性
namespace MyString
{
class Program
{
static void Main(string[] args)
{
string str1, str2;
str1 = "hello";
str2 = "world";
string str3 = str1 + str2;
Console.WriteLine("legth:{0}", str1.Length);
}
}
}
String类的常用方法
namespace MyString
{
class Program
{
static void Main(string[] args)
{
string str1 = "hello";
string str2 = "world";
string str3 = "hello";
Console.WriteLine("result is : {0}", String.Compare(str1, str3));
string str4 = "HELLO";
Console.WriteLine("Compare result: {0}", String.Compare(str1, str4, true));
Console.WriteLine("COmpare result: {0}", String.Compare(str1, str4, false));
Console.WriteLine("Concat result:{0}", String.Concat(str1, str2));
string str5 = String.Concat(str1, " ", str2);
Console.WriteLine("Concat result:{0}", str5);
Console.WriteLine("Concat result:{0}", String.Concat(str1, " ", str2, "!!!"));
Console.WriteLine("str5 have str1 is {0}", str5.Contains(str1));
Console.WriteLine("IndexOf: {0}", str1.IndexOf('o'));
Console.WriteLine("str2 in str5 of IndexOf: {0}", str5.IndexOf(str2));
Console.WriteLine("IndexOf: {0}", str5.IndexOf('o'));
Console.WriteLine("IndexOf: {0}", str5.IndexOf('o', 5));
char[] arr1 = new char[3] { 'l', 'o', 'w' };
Console.WriteLine("IndexOfAny: {0}", str5.IndexOfAny(arr1));
Console.WriteLine("IndexOfAny: {0}", str5.IndexOfAny(arr1, 5));
str1 = str1.Insert(2, str4);
Console.WriteLine("str1:{0}", str1);
Console.WriteLine("str1 isNullOrEmpty:{0}", String.IsNullOrEmpty(str1));
Console.WriteLine("LastIndexOf: {0}", str5.LastIndexOf('o'));
Console.WriteLine("str2 in str5 of LastIndexOf: {0}", str5.LastIndexOf(str2));
Console.WriteLine("Rrmove: {0}", str3.Remove(2));
Console.WriteLine("Rrmove: {0}", str3.Remove(2, 2));
Console.WriteLine("Replace: {0}", str3.Replace('l', 'w'));
Console.WriteLine("Replace: {0}", str5.Replace("world", "WORLD"));
char[] arr2 = str3.ToCharArray();
foreach (char i in arr2)
{
Console.Write("{0} ", i);
}
Console.WriteLine(" ");
char[] arr3 = str3.ToCharArray(2, 3);
foreach (char i in arr3)
{
Console.Write("{0} ", i);
}
Console.WriteLine(" ");
Console.WriteLine("ToLower:{0}", str4.ToLower());
Console.WriteLine("ToUpper:{0}", str3.ToUpper());
string str6 = " h e l l o ";
Console.WriteLine("Trim:{0}", str6.Trim());
Console.WriteLine("Trim:{0}", str6);
}
}
}