C#基础知识学习(2)string类中的方法
生活随笔
收集整理的這篇文章主要介紹了
C#基础知识学习(2)string类中的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.Compare 比較字符串
用來比較2個字符串的長度大小和值是否相同,相同則返回0,當x比y小返回-1,否則返回1,如果長度相同,且值不同,則返回1,代碼如下 public static void Main() { string x = "nihao"; string y = "nihao ma";結(jié)果:-1 //2.string x = "nihao ma"; //string y = "nihao";結(jié)果: 1 //3.string x = "nihao"; //string y = "nihao";結(jié)果: 0 //4.string x = "niliu"; //string y = "nihao";結(jié)果: 1 int result = string.Compare(x,y); Console.WriteLine("結(jié)果:{0}",result); Console.ReadKey(); } 2.String.CompareOrdinal?比較字符(還沒有搞清楚,歡迎各位補充) 通過計算每個字符串中相應?System.Char?對象的數(shù)值來比較兩個指定的?System.String?對象public?static?int?CompareOrdinal(string?strA,?string?strB);? ? public?static?int?CompareOrdinal(string?strA,?int?indexA,?string?strB,?int?indexB,?int?length);? 通過計算每個字符串中相應?System.Char?對象的數(shù)值來比較兩個指定的?System.String?對象??
? 3.Concat 連接字符串 public?static?string?Concat(paramsstring[]?values);
用來連接多個字符串實例 string x="你好"; string y=",歡迎你"; string z=string.Concat(x,y); 結(jié)果是:你好,歡迎你 Concat和+的不同: Concat只能用來連接字符串,同時是方法名。
+號是運算符,可以連接多種類型。 ? 4.CopyTo public?void?CopyTo(拷貝intsourceIndex,char[]?destination,intdestinationIndex,intcount);
這個方法理解起來有點難度,只是找到下面的例子
string dest = "Hello world"; string source = "Goodbye China"; char[] destArray = dest.ToCharArray();//將dest變成字符數(shù)組 source.CopyTo(8, destArray, 6, 5);//從source的第8個字符起復制5個字符并從destArray的第6個位置開始放 dest = new string(destArray);//這時dest為"Hello China" Console.WriteLine(dest); 輸出結(jié)果是:Hello China
? 5. IndexOf 索引字符的位置 定位字符
- intIndexOf(charvalue)
- intIndexOf(charvalue,intstartIndex)
- intIndexOf(charvalue,intstartIndex,intcount)
定位字符串:
?
?
在上述重載形式中,其參數(shù)含義如下:
Value:待定位的字符或者子串。
startIndex:在總串中開始搜索的起始位置。
Count:在總串中從起始位置開始搜索的字符數(shù)。
1)一個參數(shù),索引字符第一次出現(xiàn)的位置,返回int? 例子: String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); Console.WriteLine("結(jié)果是{0}",x); 結(jié)果是4,也就是說o在字符串str1中的位置是4. indexof(string str,int i) 2)二個參數(shù),表示從i+1的位置開始索引。 String str1 = "hello world"; String str2 = "abcd"; int x = str1.IndexOf("o"); int y = str1.IndexOf("o",5); Console.WriteLine("結(jié)果是{0},定索引位置的索引結(jié)果是{1}",x,y); Console.ReadKey(); 結(jié)果是:7 定位從第5個開始搜索,搜索到o的位置是7,返回int 是7 3)三個參數(shù),確定開始和要查的幾位數(shù)的位置 indexof(string str,int i,int j) String str1 = "hello world or happy you "; int z = str1.IndexOf("o",10,4); Console.WriteLine("倒序索引結(jié)果{0}",z); 從第10位開始查找,查詢2位,結(jié)果是12 ? 6.LastIndexOf 索引最后的位置 定位最后一次的位置,如果是三個參數(shù),就是從后往前索引。 ? 7.IndexOfAny 表示索引字符數(shù)組中,最近的一個值的位置。 String str1 = "hello world or happy you "; char[] b = { 'e', 'o', 'l' }; int a = str1.IndexOfAny(b,5,15); Console.WriteLine("結(jié)果是{0}}",a); 返回int為7 ? 8.Insert 插入字符串 public?string?Insert(intstartIndex,stringvalue);把一個字符串插入到另一個字符串的指定位置 String str1 = "hello world or happy you "; String str2 = "abcd"; string str3 = str1.Insert(2, str2); Console.WriteLine("insert功能插入結(jié)果{0}",str3); 這里是吧str2插入到str1中。插入位置2得到結(jié)果heabcdllo world or happy you ? 9.Join 在字符串數(shù)組中插入指定的字符 str5必須是字符串數(shù)組 ? string[] str5 = {"fsa","fasdf","fsaf"}; string str4 = string.Join("/", str5); Console.WriteLine("Join功能結(jié)果{0}", str4); 輸出結(jié)果fsa/fasdf/fsaf ? ? 10.PadLeft和PadRight
PadLeft是指在左面插入指定字符串總長度的char類型 string str1 = "hello world"; char str2='a'; string str3 = str1.PadLeft(12,str2); Console.WriteLine("PadLeft功能插入結(jié)果{0}", str3); 結(jié)果是:ahello world PadRight是指在右面插入指定字符串總長度的char類型
string str1 = "hello world"; char str2='a'; string str3 = str1.PadRight(12,str2); Console.WriteLine("PadLeft功能插入結(jié)果{0}", str3); 結(jié)果是:hello worlda
? 11.Replace?替換字符串
?public?string?Replace(char?oldChar,?char?newChar);
?public?string?Replace(string?oldValue,?string?newValue);
string str1 = "hello world"; str1 = str1.Replace("d", "d!"); Console.WriteLine("Replace功能插入結(jié)果{0}", str1); str1要更改的字符串,第一個參數(shù)是要更改的字符,第二個參數(shù)更新后的字符 結(jié)果是:hello world! ? 12.Split?分隔字符串 public?string[]?Split(paramschar[]?separator);string str1 = "hello world"; string[]?str2?=?str1.Split('w'); ?結(jié)果是:hello world! 字符串數(shù)組,需要用for遍歷獲取,第一個參數(shù)是old,第二個參數(shù)是new 如果想獲取分割后的單個值,可以這樣使用 string str1 = "hello world"; string str2 = str1.Split('w')[0]; Console.WriteLine("分割數(shù)據(jù){0}",str2); 這樣就相當于獲取數(shù)組中第一個值hello,如果是[1],那么獲取的就是orld。 ? 13.remove移除指定位置字符串 ?public?string?Remove(int?startIndex);? 移除的位置從sartIndex到結(jié)束位置的字符串 ?public?string?Remove(int?startIndex,?int?count);?
移除startIndex位置到count位置的字符串 string str1 = "changed"; string str4 = str1.Remove(1, 2); str4得到的是cnged,移除了第二位和第三位。 ? 14.Substring這個方法用的比較多(截取字符串) String.Substring (Int32)從此實例檢索子字符串。子字符串從指定的字符位置開始。 string s = "Hello C# World!"; string s1=s.Substring(3); Console.WriteLine(s1); 結(jié)果是:lo C# World!??? //s1為從s中截取的位置為3的字符以后的字符子串,表示從第4位開始截取字符。
String.Substring (Int32, Int32)?從此實例檢索子字符串。子字符串從指定的字符位置開始且具有指定的長度。
string s = "Hello C# World!"; string s1=s.Substring(3,2); Console.WriteLine(s1); 結(jié)果是lo,后面一個參數(shù)限制了截取的位數(shù)。 ? ? ? ? ? ? ?
轉(zhuǎn)載于:https://www.cnblogs.com/myhomebo/p/5481307.html
總結(jié)
以上是生活随笔為你收集整理的C#基础知识学习(2)string类中的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TCP/IP详解学习笔记(5)-IP选路
- 下一篇: c# char unsigned_dll