c#常用的字符串函數
例一:
獲取字符串的大小寫函數
ToLower():得到字符串的小寫形式
ToUpper():得到字符串的大寫形式
注意:
字符串時不可變的,所以這些函數都不會直接改變字符串的內容,而是把修改后的字符串通過函數返回值的形式返回。
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = "GOOD"; s=s.ToLower();//s.ToLower():返回值為字符串的小寫 Console.WriteLine(s);/* 不是改變了字符串的內容,而是生成一個新的全部變為小寫的字符串,然后用s指向這個新的字符串。*/ Console.WriteLine(s.ToUpper());//s.ToUpper()返回值為字符串的大寫。 Console.ReadKey(); } } }
運行截圖:

例二:
字符串去兩邊的空白函數
Trim();
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = " GOOD "; Console.WriteLine("去兩邊空白函數使用前:\n|{0}|",s);//調用函數前字符串兩邊有空白 s=s.Trim(); Console.WriteLine("去兩邊空白函數使用后:\n|{0}|", s);//調用函數后字符串兩邊無空白 Console.ReadKey(); } } }
程序截圖:

例三:
字符串忽略大小寫比較的函數
"abc"=="ABC"
bool string.Equals(string value,StringComparsion comparisonType)(+2重載)
確定此字符串是否與指定的System.String對象具有相同的值。
參數指定區域性,大小寫以及比較所用的排序規則。
異常:
system.NULLReferenceException
system.ArgumentException
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { /* ********************************************* * bool string.Equals(string value,StringComparsion comparisonType)(+2重載) * 確定此字符串是否與指定的System.String對象具有相同的值。 * 參數指定區域性,大小寫以及比較所用的排序規則。 * 異常: * system.NULLReferenceException * system.ArgumentException ************************************************ * StringComparsion枚舉類型 * bool b="abc"=="ABC"; * Ignore:區分,Case:大小寫 *==是區分大小寫的比較,Equals("ABC", StringComparison.OrdinalIgnoreCase)是忽略大小寫的比較. */ //判斷"abc"=="ABC"返回值bool型為false,忽略大小函數調用后此bool型為true bool b = "abc".Equals("ABC", StringComparison.OrdinalIgnoreCase); Console.WriteLine(b);//由於調用了忽略大小寫函數,所以bool型返回值結果為true Console.ReadKey(); } } }
運行結果:

例四:
字符串的分割函數:
string[] Split(params char[] separator):
將字符串按照指定的分隔符分割為字符串數組
將字符串按照指定的分隔符分割為字符串數組函數:
1.將aaa,bbb,ccc,dddfdsajf按照‘,’進行分隔
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = "aaa,bbb,ccc,dddfdsajf"; /* string[] Split(params char[] separator): * 將字符串按照指定的分隔符分割為字符串數組 */ string[] strs = s.Split(',');//將字符串s按照','進行分割成字符串數組。 //循環打印字符串數組strs中的內容 foreach(string item in strs) { Console.WriteLine(item); } Console.ReadKey(); } } }
程序截圖:

2.移除結果中的空白字符的分隔函數
string[] Split(charp[] separator,StringSplitOptions options)
將字符串按照指定的char分隔為字符串數組(option取 RemoveEmptyEntrles的時候移除結果中的空白字符)
將aaa,bbb,,ccc,dddfdsajf進行分隔,如果用上面那個方法進行分隔則會出現空白字符:

為了避免以上情況,可以使用這個函數解決。
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = "aaa,bbb,,ccc,dddfdsajf"; /* string[] Split(charp[] separator,StringSplitOptions options) * 將字符串按照指定的char分隔為字符串數組 * * (option取 RemoveEmptyEntrles的時候移除結果中的空白字符) */ string[] strs = s.Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries); //循環打印字符串數組strs中的內容 foreach(string item in strs) { Console.WriteLine(item); } Console.ReadKey(); } } }
程序運行結果:

3.字符串作為分隔符進行分隔字符串函數:
string[] Split(string[] separator,StringSplitOptions options)
將字符串按照指定的string分隔符分割為字符串數組。
如:將"我是星雲我是fairy我是顏可"分隔成"星雲fairy顏可"
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = "我是星雲我是fairy我是顏可"; /* string[] Split(new string[] separator,StringSplitOptions options) * 將字符串按照指定的char分隔為字符串數組 * * (option取 RemoveEmptyEntrles的時候移除結果中的空白字符) */ string[] strs = s.Split(new string[] {"我是"},StringSplitOptions.RemoveEmptyEntries); //循環打印字符串數組strs中的內容 foreach(string item in strs) { Console.WriteLine(item); } Console.ReadKey(); } } }
程序運行截圖:

練習一:
接受用戶輸入的一句英文,將其中的單詞反序輸出。
"hello c sharp"——>"sharp c hello"
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string s = Console.ReadLine();//接受一個字符串 string[] words = s.Split(' ');//將字符串進行分隔 for (int i = words.Length - 1; i >= 0; i--)//反序輸出 { Console.Write(words[i] + " "); } Console.ReadKey(); } } }
程序運行截圖:

練習二:
所用函數:
int string.IndexOf(char value)(+8重載)
報告指定Unicode字符在此字符串中的第一個匹配項的索引。string string.Substring(int startIndex,int length)(+1重載)
從此實例檢索字符串中子字符串從指定的字符位置開始,且指有指定的長度.異常:
System.ArgumentOutofRangException
目標:將xingyun2684@gmail.com進行分隔,分隔出xingyun2684和gmail.com
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string email= Console.ReadLine();//接受一個Email字符串,如xingyun2684@gmail.com /* ********************************************* * int string.IndexOf(char value)(+8重載) * 報告指定Unicode字符在此字符串中的第一個匹配項的索引。 ************************************************* */ int atIndex = email.IndexOf('@');//取@所在的位置 /************************************************ * string string.Substring(int startIndex,int length)(+1重載) * 從此實例檢索字符串中子字符串從指定的字符位置開始,且指有指定的長度. * 異常: * System.ArgumentOutOfRangeException *********************************************** */ string username = email.Substring(0,atIndex);//獲取從開始位置到@所在位置的前一個位置的字符串,即xingyun2684 string 域名 = email.Substring(atIndex + 1);//獲取@所在位置+1的字符串,即gmail.com Console.WriteLine(username);//打印xignyun2684 Console.WriteLine(域名);//打印gmail.com Console.ReadKey(); } } }
程序截圖:

練習三:
標題作者顯示,直間。。。分隔

源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines(@"d:\2.txt", Encoding.Default); foreach (string line in lines) { string[] strs=line.Split(new char[] {' '},StringSplitOptions.RemoveEmptyEntries); string title = strs[0];//標題 string author = strs[1];//作者 /* Math.Min(a, title.b); * 返回a和b兩個數的最小值。 */ title = title.Substring(0, Math.Min(18, title.Length)); title = title + "..."; Console.WriteLine("{0}{1}", title, author); } Console.ReadKey(); } } }
運行截圖:

練習四:

讀取ini配置文件內容,函數傳參數實現查詢項目名對象的值。
源碼如下:
using System; using System.Collections.Generic; using System.Text; namespace 字符串函數學習 { class Program { static void Main(string[] args) { string value = GetConfigValue(@"d:\3.ini", "ip");//函數調用傳參數,文件路徑和項目名 Console.WriteLine(value); Console.ReadKey(); } static string GetConfigValue(string filename, string itemName) { string[] lines = System.IO.File.ReadAllLines(filename, Encoding.Default);//讀取一個文件 foreach (string line in lines) //一行一行讀 { string[] strs = line.Split('=');//將字符串按照=進行分隔 string name = strs[0];//ip string value = strs[1];//192.168.1 if (name.Trim() == itemName)//判斷查詢是否相同 { return value.Trim(); } } return "沒找到或文件路徑不正確!"; } } }
程序截圖:

