C#(一)-字串的處理
前言
剛開始還有點跟不上小楊老師的速度,主要還是剛開始C#的學習還不夠熟練,小楊老師說的對,不能光指著課上講的,課下自己要多加練習。
知識點
1.string可以看作是char的只讀陣列。char c = s[1];.例子:遍歷string中的每個元素。
2.C#中字串有一個重要的特性:不可變性,字串一旦宣告就不再可以改變。所以只能通過索引來讀取指定位置的char,不能對指定位置的char進行修改。
3.如果要對char進行修改,那麼就必須建立一個新的字串,用s.ToCharArray()方法得到字串的char陣列,對陣列進行修改後,呼叫new string(char[])這個建構函式(暫時不用細研究)來建立char陣列的字串。一旦字串被建立,那麼char陣列的修改也不會造成字串的變化。例子:將字串中的A替換為a。
4.string str = null與string str = ""說明其中的區別?
string str = null是不給他分配記憶體空間,而string str = "“給它分配長度為空字串記憶體空間。string str = null沒有string物件,string str =”"有一個字串物件。
5.StringBuilder和String的區別?
String在進行運算時(如賦值、拼接)會產生一個新的例項,而StringBuilder則不會。所以在大量字串拼接或頻繁對某一字串進行操作時,最好使用StringBuilder,不要使用String.
如果操作一個不斷增長的字串,儘量不用String類,改用StringBuilder類。
練習
1.隨機輸入你心中想到的一個名字,然後輸出它的字串長度 Length:可以得到字串的長度。
Console.WriteLine("輸入你心中想到的名字");
string name = Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadKey();
2.兩個學員輸入各自最喜歡的課程名稱,判斷是否一致,如果相同,則輸出你們倆喜歡相同的課程,如果不相同,則輸出你們倆喜歡不同相同的課程。
Console.WriteLine("輸入第一門課程"); string str1 = Console.ReadLine(); Console.WriteLine("輸入第二門課程"); string str2 = Console.ReadLine(); if(str1==str2) { Console.WriteLine("課程一樣" + str1); } else { Console.WriteLine("課程不同{0}-----------{1}", str1, str2); } Console.ReadKey(); 轉換成小寫再比較: str1=str1.ToLower(); str2=str2.ToLower(); 轉換成大寫再比較: str1 = str1.ToUpper(); str2 = str2.ToUpper(); 忽略大小寫的比較: string str1 = Console.ReadLine(); Console.WriteLine("輸入第二門課程"); string str2 = Console.ReadLine(); bool result = str1.Equals(str2, StringComparison.OrdinalIgnoreCase); if(result) { Console.WriteLine("課程相同"); } else { Console.WriteLine("課程不相同"); } Console.ReadKey();
3.移除字串中不想要的內容
string str = "哈---哈,我 又 變 帥 了";
char[] chs = new char[] { ' ', '-' };
string[] result = str.Split(chs,StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i< result.Length; i++)
{
Console.Write(result[i]);
}
Console.ReadKey();
移去字串中不想要的內容,然後顯示年月日:
string date = "2012-----------11-------02";
char[] chs = new char[] { '-' };
string[] time = date.Split(chs, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年{1}月{2}日", time[0], time[1], time[2]);
Console.ReadKey();
4.把字串中某些字元或者字串替換掉:
string name = "小楊很邪惡";
name = name.Replace('很','不');
Console.WriteLine(name);
Console.ReadKey();
string name = "小楊很邪惡";
name = name.Replace("小楊", "老大");
Console.WriteLine(name);
Console.ReadKey();
5.判斷字串中是否包含子字串:
string name = "小楊很邪惡";
bool result = name.Contains("小楊");
if (result)
{
Console.WriteLine("字串中包含這個字串");
}
else
{
Console.WriteLine("不包含這個字串");
}
Console.ReadKey();
還有一種:
bool result = name.Contains("小楊");
if (result)
{
name=name.Replace("邪惡", "純潔");
Console.WriteLine(name);
}
else
{
Console.WriteLine(name);
}
Console.ReadKey();
7.找到某個字串索引再擷取:
string str = "哈哈,小楊又變帥了";
str=str.Substring(3,6);//擷取字串,不能超出字串的長度,否則會報錯。
Console.WriteLine(str);
Console.ReadKey();
倒著擷取:
string path = @"C:\Users\15128\Desktop";
string str = path.Substring(path.Length - 7);
Console.WriteLine(str);
Console.ReadKey();
可以把Desktop截取了。
以\為單位擷取:
string path = @"C:\Users\15128\Desktop";
string[] str = path.Split(new char[] { '\\' });
Console.WriteLine(str[str.Length - 1]);
Console.ReadKey();
7.判斷字串是否以某個字串開始:
string str = ("小楊很純潔");
bool result = str.StartsWith("小");
if (result)
{
Console.WriteLine("有這個字串");
}
else
{
Console.WriteLine("沒有這個字串");
}
Console.ReadKey();
判斷字串是否以某個字串結束:
string str = ("小楊很純潔");
bool result = str.EndsWith("潔");
if (result)
{
Console.WriteLine("有這個字串");
}
else
{
Console.WriteLine("沒有這個字串");
}
Console.ReadKey();
8.找字串的索引:
string str = ("小楊很純潔");
int index = str.IndexOf("純");*///如果這個字串在這個字串中,那麼就會把這個字串的索引顯示出來,那麼,如果找不到這個字串返回的結果是-1
Console.WriteLine(index);
Console.ReadKey();
找最後一個字串的索引:
string str = "fdsafonfdnfisf";
int index = str.LastIndexOf("f");
Console.WriteLine(index);
Console.ReadKey();
擷取檔名:
string path = @"C:\Program Files(x86)\Thunder Network\Thunder\Program\Thunder.exe -StartType:DesktopIcon";
int index = path.LastIndexOf("\\");
string st = path.Substring(index + 1);
Console.WriteLine(st);
Console.ReadKey();
在指定的索引處新增字串:
string st = "哈哈,我變帥了"
st = st.Insert(7, "又");
Console.WriteLine(st);
Console.ReadKey();
方法
1.接收使用者輸入的字串,將其中的字元以與輸入相反的順序輸出。
Console.WriteLine("輸入內容");
string text = Console.ReadLine();
for(int i = text.Length -1;i>=0;i--)
{
Console.Write(text[i]);
}
Console.ReadKey();
2.接收使用者輸入的一句英文,將其中的單詞以反序輸出。
Console.WriteLine("輸入一句話");
string text = Console.ReadLine();
string[] strs = text.Split();
string st = "";
for(int i =strs.Length -1;i>=1;i--)
{
st += strs[i] + " ";
}
Console.WriteLine(st + strs[0]);
Console.ReadKey();
3.從Email中提取出使用者名稱和域名:[email protected]
string email = "[email protected]";
string[] sts = email.Split('@');
Console.WriteLine("使用者名稱是{0}", sts[0]);
Console.WriteLine("域名是{0}", sts[1]);
Console.ReadKey();
4.讓使用者輸入一句話,判斷這句話中有沒有邪惡,如果有邪惡就替換成這種形式然後輸出:
Console.WriteLine("請輸入你想到的一句話");
string text = Console.ReadLine();
if (text.Contains("邪惡"))
{
Console.WriteLine(text.Replace("邪惡", "**"));
}
else
{
Console.WriteLine(text);
}
Console.ReadKey();
5.把{“諸葛亮”,“鳥叔”,“卡卡西”,“卡哇伊”}變成諸葛亮|鳥叔|卡卡西|卡哇伊,然後再把|切割掉:
string[] names = { "諸葛亮", "鳥叔", "卡卡西", "卡哇伊" };
string st = string.Join("", names);
Console.WriteLine(st);
Console.ReadKey();
6.從某個地方開始移除,移除多少個字串:
string text = "老楊果然很邪惡";
text = text.Remove(5, 2);
Console.WriteLine(text);
Console.ReadKey();