C#中字串的常用屬性和方法
本篇文章小編總結在C#中,對於字串型別我們常用的屬性和方法:
【1】構造方法:
通過構造方法我們可以根據自己的需要構建一個字串變數(或字串型別的物件例項)。
案例1)通過構造方法,宣告並賦值一個由20個字元‘H’組成的字串,並輸出。
程式碼:
string Str=new string('H',20); //新建立一個string型別的物件,並使用構造方法(函式)構建一個由20個H組成的字串並賦值給Str
Console.WriteLine(Str);
Console.ReadKey();
執行結果:
案例2)通過構造方法,將一個字元陣列{'H','e','l','l','o',',','W','o','r','l','d'}構建成一個字串並賦值。
string Str=new string(new char[]{'H','e','l','l','l','o',',','W','o','r','l','d'});
Console.WriteLine(Str);
Console.ReadKey();
執行結果:
【2】Length屬性,得到字串的字元數。
案例1)得到字串“Hello,world!”的字元數。
string Str="Hello,world!";
Console.WriteLine("\"Hello,world!\"的字元數為:{0}",Str.Length);
Console.ReadKey();
執行結果:
【3】ToUpper()方法,字串中的所有小寫字母轉換成大寫。
案例1)將字串“Hello,World!”中的小寫字母轉換為大寫並輸出轉換後的字串。
string Str="Hello,World!";
string StrNew=Str.ToUpper();
Console.WriteLine("轉換前的字串為:{0}",Str);
Console.WriteLine("轉換後的字串為:{0}",StrNew);
Console.ReadKey();
執行結果:
【4】ToLower()方法,字串中的所有大寫字母轉換成小寫。
案例1)將字串“Hello,World!”中的小寫字母轉換成大寫並輸出轉換後的字串。
string Str="Hello,World!";
string StrNew=Str.ToLower();
Console.WriteLine("轉換前的字串為:{0}",Str);
Console.WriteLine("轉換後的字串為:{0}",StrNew);
Console.ReadKey();
執行結果:
【5】比較兩個字串是否相同,可以通過引數選擇比較模式(不區分大小寫還是區分大小寫)。
案例1)用不區分大小寫模式比較兩個字串“Hello,World!”和“hello,world!”。
string Str1="Hello,World!";
string Str2="hello,world!";
//不區分大小寫模式
if(Str1.Equals(Str2,StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("比較的兩個字串是相同的!");
}
else
{
Console.WriteLine("比較的兩個字串是不相同的!");
}
Console.ReadKey();
執行結果:
案例2)用區分大小寫模式比較兩個字串“Hello,World!”和“hello,world!”。
string Str1="Hello,World!";
string Str2="hello,world!";
//區分大小寫模式
if(Str1.Equals(Str2,StringComparison.Ordinal))
{
Console.WriteLine("比較的兩個字串是相同的!");
}
else
{
Console.WriteLine("比較的兩個字串是不相同的!");
}
Console.ReadKey();
執行結果:
【6】Split()方法,分割字串,返回字串型別的陣列。
案例1)分割字串“我 是&C* #初( 學|者”組成一個“我是C#初學者”的字串。
string Str="我 是&C* #初( 學|者";
char[] Chr={' ','&','(','|','*'};
string[] StrNew = Str.Split(Chr, StringSplitOptions.RemoveEmptyEntries); //分割字串組成字串陣列,並移除空字串
string Str1="";
for(int i=0;i<StrNew.Length;i++)
{
Str1+=StrNew[i];
}
Console.WriteLine("您想要得到的字串是:{0}",Str1);
Console.ReadKey();
執行結果:
【7】Substring()方法,擷取字串,在擷取的時候包含擷取的索引位置。
案例一)擷取字串“大家好,我是C#初學者”中的“我是C#初學者”,並輸出擷取前和擷取後的字串。
string StrOld="大家好,我是C#初學者";
string StrNew=StrOld.Substring(4);
Console.WriteLine("擷取之前的字串為:{0}",StrOld);
Console.WriteLine("擷取之後的字串為:{0}",StrNew);
Console.ReadKey();
執行結果:
【8】IndexOf()方法,判斷某個字串在字串中第一次出現的位置的索引,沒有則返回-1。
案例1)查詢"C#"在,“大家好,我是C#初學者!”的字串中的位置。(在第幾個字元)。
String Str1="大家好,我是C#初學者!";
int Position;
Position=Str1.IndexOf("C#");
Position+=1;
Console.WriteLine("\"C#\"在\"大家好,我是C#初學者!\"的第{0}個位置。",Position);
Console.ReadKey();
執行結果:
【9】LastIndexOf()方法,判斷某個字串在字串中最後出現的位置的索引,沒有則返回-1。
案例1)輸出字串“C#”在字串“我愛C#,你愛C#,我們大家都愛C#語言!”中最後一次出現的位置。
string Str="我愛C#,你愛C#,我們大家都愛C#語言!";
int Position;
Position=Str.LastIndexOf("C#");
Position+=1;
Console.WriteLine("\"C#\"在\"我愛C#,你愛C#,我們大家都愛C#語言!\"出現的位置為{0}",Position);
Console.ReadKey();
執行結果:
【10】判斷字串是否以指定子字串開始。
案例1)判斷“大家好,我是C#初學者!”是否以“大家好”開始,如果是則輸出是,如果不是則輸出不是。
string Str="大家好,我是C#初學者!";
if(Str.StartsWith("大家好"))
{
Console.WriteLine("是的");
}
else
{
Console.WriteLine("不是的");
}
Console.ReadKey();
執行結果:
【11】判斷字串是否以指定子字串結束。
案例1)判斷“大家好,我是C#初學者!”字串是否以“!”結束。
string Str="大家好,我是C#初學者!";
if(Str.EndsWith("!"))
{
Console.WriteLine("是的");
}
else
{
Console.WriteLine("不是的");
}
Console.ReadKey();
執行結果:
【12】將字串中指定的子字串替換成你想要的字串。
案例1)將“大家好,我是C#初學者!”字串中的“!”替換成“?”。
string Str="大家好,我是C#初學者!";
string StrNew=Str.Replace("!","?");
Console.WriteLine("替換前的字串為:{0}",Str);
Console.WriteLine("替換後的字串為:{0}",StrNew);
Console.ReadLine();
執行結果:
【13】Contains()方法,判斷字串是否包含指定的子字串。
案例1)判斷字串“大家好,我是C#初學者!”字串中是否包含子字串“C#”,如果包含輸出“包含”,如果不包含輸出“不包含”。
string Str="大家好,我是C#初學者!";
if(Str.Contains("C#"))
{
Console.WriteLine("包含!");
}
else
{
Console.WriteLine("不包含!");
}
Console.ReadKey();
執行結果:
【14】Trim()方法,去掉字串前後的空格。
案例1)去掉字串“ 我是C#初 學者 ”的前後空格。
string Str=" 我是C#初 學者 ";
string StrNew=Str.Trim();
Console.WriteLine("去掉前後空格之前的字串為:{0}",Str);
Console.WriteLine("去掉前後空格之後的字串為:{0}",StrNew);
Console.ReadKey();
執行結果:
【15】TrimStart()和TrimEnd()方法,分別是去掉字串前面的空格和去掉字串後面的空格。
案例1)分別去掉“ 我是C#初學者 ”,並分別輸出。
string Str=" 我是C#初學者 ";
string Str1=Str.TrimStart();
Console.WriteLine("去掉之前的字串為:{0}",Str);
Console.WriteLine("去掉字串前面的空格之後為:{0}",Str1);
string Str2=Str1.TrimEnd();
Console.WriteLine("去掉字串後面的空格之後為:{0}",Str2);
Console.ReadKey();
執行結果:
【16】string.IsNullOrEmpty()方法,判斷一個字串是否為空。
案例1)判斷“你好”字串是否為空。
string Str="你好";
if(string.IsNullOrEmpty(Str))
{
Console.WriteLine("字串為空");
}
else
{
Console.WriteLine("字串不為空");
}
Console.ReadKey();
執行結果:
【17】string.Join()方法,將陣列按照指定的字串連線,並返回一個字串。
案例1)將字串陣列{"大","家","好"}輸出為“大|家|好”的形式。
string[] Str={"大","家","好"};
string StrNew=string.Join("|",Str);
Console.WriteLine(StrNew);
Console.ReadKey();
執行結果: