在C#中,可以使用字符串作为字符数组,但是,更常见的做法是使用string关键字声明字符串变量,string关键字是System.String类的别名。
using System; namespace StringApplication { class Program { static void Main(string[] args) { //来自字符串文字和字符串连接 string fname, lname; fname = "Rowan"; lname = "Atkinson"; char []letters= { 'H', 'e', 'l', 'l','o' }; string [] sarray={ "Hello", "From", "Learnfk", "Point" }; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname); //by using string constructor { 'H', 'e', 'l', 'l','o' }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //methods returning string { "Hello", "From", "Learnfk", "Point" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message); //formatting method to convert a value DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); } } }
编译并执行上述代码时,将生成以下输出-
Full Name: RowanAtkinson Greetings: Hello Message: Hello From Learnfk Point Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
String类具有以下两个属性-
Sr.No. | Property & 描述 |
---|---|
1 | Chars 获取当前字符串对象中指定位置的CHAR对象。 |
2 | Length 获取当前String对象中的字符数。 |
String类有许多方法可以帮助您使用String字符串对象。下表提供了一些最常用的方法-
Sr.No. | Methods & 描述 |
---|---|
1 | public static int Compare(string Stra,string strb) 比较两个指定的字符串对象并返回一个整数,该整数指示它们在排序顺序中的相对位置。 |
2 | public static int Compare(String Stra,String STRB,bool gnnoreCase) 比较两个指定的字符串对象并返回一个整数,该整数指示它们在排序顺序中的相对位置。 |
3 | public static String concat(string str0,string str1) 连接两个字符串对象。 |
4 | public static String concat(string str0,string str1,string str2) 连接三个字符串对象。 |
5 | public static String concat(string str0,string str1,string str2,string str3) 连接四个字符串对象。 |
6 | public bool Contain(String value) 返回一个值,该值指示指定的字符串对象是否出现在此字符串中。 |
7 | public static string Copy(String str) 创建与指定字符串具有相同值的新字符串对象。 |
8 | public void CopyTo(int sourceIndex,char[]destination,int destinationIndex,int count) 将指定数量的字符从字符串对象的指定位置复制到Unicode字符数组中的指定位置。 |
9 | public bool EndsWith(String value) 确定字符串对象的结尾是否与指定的字符串匹配。 |
10 | public bool Equals(String value) 链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/csharp-strings.html 来源:LearnFk无涯教程网 确定当前字符串对象和指定的字符串对象是否具有相同的值。 |
11 | public static boo Equals(String a,String b) 确定两个指定的字符串对象是否具有相同的值。 |
12 | public static string Format(string format,object arg0) 用指定对象的字符串表示形式替换指定字符串中的一个或多个格式项。 |
13 | public int IndexOf(Char Value) 返回当前字符串中指定Unicode字符的第一个匹配项的从零开始的索引。 |
14 | public int IndexOf(string value) 返回此中指定字符串的第一个匹配项的从零开始的索引。 |
15 | public int IndexOf(char value,int startIndex) 返回此字符串中指定Unicode字符的第一个匹配项的从零开始的索引,从指定的字符位置开始搜索。 |
16 | public int IndexOf(string value,int startIndex) 返回此中指定字符串的第一个匹配项的从零开始的索引,从指定字符位置开始搜索。 |
17 | public int IndexOfAny(char[]anyof) 返回指定Unicode字符数组中任何字符在此中的第一个匹配项的从零开始的索引。 |
18 | public int IndexOfAny(char[]anyof,int startIndex) 返回指定Unicode字符数组中任何字符在此中的第一个匹配项的从零开始的索引,从指定字符位置开始搜索。 |
19 | public string Insert(int startIndex,string value) 返回一个新字符串,其中指定的字符串插入到当前String对象中的指定索引位置。 |
20 | public static bool IsNullOrEmpty(string value) 指示指定的字符串是null还是空字符串。 |
21 | public static string Join(string separator,params string[]value) 在每个元素之间使用指定的分隔符连接字符串数组的所有元素。 |
22 | public static string Join(String deparator,String[]value,int startIndex,int count) 在每个元素之间使用指定的分隔符连接字符串数组的指定元素。 |
23 | public int LastIndexOf(Char Value) 返回当前字符串对象中指定的Unicode字符的最后一个匹配项的从零开始的索引位置。 |
24 | public int LastIndexOf(string value) 返回当前字符串对象中指定字符串的最后一个匹配项的从零开始的索引位置。 |
25 | public string Remove(Int StartIndex) 移除当前中的所有字符(从指定位置开始,一直持续到最后一个位置),并返回字符串。 |
26 | public string Remove(int startIndex,int count) 移除当前字符串中从指定位置开始的指定数量的字符,并返回该字符串。 |
27 | public string Replace(char oldChar,char newChar) 将当前字符串对象中所有出现的指定Unicode字符替换为指定的Unicode字符,并返回新字符串。 |
28 | public string replace(string oldValue,string newValue) 将当前字符串对象中所有出现的指定字符串替换为指定的字符串,并返回新字符串。 |
29 | public string[]split(params char[] separator) 返回一个字符串数组,该数组包含当前字符串对象中的子字符串,由指定Unicode字符数组的元素分隔。 |
30 | public string[]split(char[]separator ,int count) 返回一个字符串数组,该数组包含当前String对象中的子字符串,由指定Unicode字符数组的元素分隔。 |
31 | public bool startswith(string value) 确定此字符串的开头是否与指定的字符串匹配。 |
32 | public char[] ToCharArray() 返回一个Unicode字符数组,其中包含当前字符串对象中的所有字符。 |
33 | public char[] ToCharArray(int startIndex,int length) 返回一个Unicode字符数组,其中包含当前字符串对象中的所有字符,从指定的索引开始,直到指定的长度。 |
34 | public string ToLower() 返回转换为小写的此字符串的副本。 |
35 | public string ToUpper() 返回转换为大写的此字符串的副本。 |
36 | public string Trim() 从当前字符串对象中移除所有前导和尾随空白字符。 |
您可以访问MSDN库以获得方法和字符串类构造函数的完整列表。
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str1 = "This is test"; string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else { Console.WriteLine(str1 + " and " + str2 + " are not equal."); } Console.ReadKey() ; } } }
编译并执行上述代码时,将生成以下输出-
This is test and This is text are not equal.
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) { Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } } }
编译并执行上述代码时,将生成以下输出-
The sequence 'test' was found.
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23); Console.WriteLine(substr); } } }
编译并执行上述代码时,将生成以下输出-
San Pedro
using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top", "I took a trip on a sailing ship", "And when I reached Jamaica", "I made a stop"}; string str = String.Join("\n", starray); Console.WriteLine(str); } } }
编译并执行上述代码时,将生成以下输出-
Down the way nights are dark And the sun shines daily on the mountain top I took a trip on a sailing ship And when I reached Jamaica I made a stop
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)