上一节:
下一节:

  C# 字符串

  • 字符串

    在 C# 中,您可以将字符串用作字符数组,但是,更常见的做法是使用 string 关键字声明一个字符串变量。string关键字是System.String类的别名。
  • 创建一个字符串对象

    您可以使用以下方法之一创建字符串对象-
    • 通过将字符串文字分配给String变量
    • 通过使用String类构造函数
    • 通过使用字符串串联运算符(+)
    • 通过检索属性或调用返回字符串的方法
    • 通过调用格式化方法将值或对象转换为其字符串表示形式
    以下示例演示了这一点-
    
    using System;
    
    namespace StringApplication {
    
       class Program {
       
          static void Main(string[] args) {
             //from string literal and string concatenation
             string fname, lname;
             fname = "Rowan";
             lname = "Atkinson";
                            
             char []letters= { 'H', 'e', 'l', 'l','o' };
             string [] sarray={ "Hello", "From", "Tutorials", "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", "Tutorials", "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 Tutorials Point
    Message: Message sent at 5:58 PM on Wednesday, October 10, 2012
    
  • 字符串类的属性

    String类具有以下两个属性-
    属性 描述
    Chars 获取当前String对象中指定位置的Char对象。
    Length 获取当前String对象中的字符数。
  • 字符串类的方法

    String类具有许多方法,可帮助您处理字符串对象。下表提供了一些最常用的方法-
    下面给出了String类的方法列表。
    方法 描述
    public static int Compare(string strA, string strB) 比较两个指定的字符串对象,并返回一个整数,该整数指示其在排序顺序中的相对位置。
    public static int Compare(string strA, string strB, bool ignoreCase ) 比较两个指定的字符串对象,并返回一个整数,该整数指示其在排序顺序中的相对位置。但是,如果布尔参数为true,则忽略大小写。
    public static string Concat(string str0, string str1) 连接两个字符串对象。
    public static string Concat(string str0, string str1, string str2) 连接三个字符串对象。
    public static string Concat(string str0, string str1, string str2, string str3) 连接四个字符串对象。
    public bool Contains(string value) 返回一个值,该值指示指定的String对象是否出现在此字符串中。
    public static string Copy(string str) 创建一个具有与指定字符串相同值的新String对象。
    public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count) 将指定数量的字符从String对象的指定位置复制到Unicode字符数组中的指定位置。
    public bool EndsWith(string value) 确定字符串对象的结尾是否与指定的字符串匹配。
    public bool Equals(string value) 确定当前String对象和指定的String对象是否具有相同的值。
    public static bool Equals(string a, string b) 确定两个指定的String对象是否具有相同的值。
    public static string Format(string format, Object arg0) 用指定对象的字符串表示形式替换指定字符串中的一个或多个格式项。
    public int IndexOf(char value) 返回当前字符串中第一次出现指定Unicode字符的从零开始的索引。
    public int IndexOf(string value) 返回在此实例中第一次出现的指定字符串的从零开始的索引。
    public int IndexOf(char value, int startIndex) 返回此字符串中首次出现指定Unicode字符的从零开始的索引,从指定字符位置开始搜索。
    public int IndexOf(string value, int startIndex) 返回在此实例中指定字符串首次出现的从零开始的索引,从指定字符位置开始搜索。
    public int IndexOfAny(char[] anyOf) 返回指定字符的Unicode字符数组中任何字符的第一次出现的从零开始的索引。
    public int IndexOfAny(char[] anyOf, int startIndex) 从指定字符位置开始搜索,返回此实例中任何字符在指定Unicode字符数组中首次出现的从零开始的索引。
    public string Insert(int startIndex, string value) 返回一个新字符串,其中在当前字符串对象的指定索引位置插入了指定字符串。
    public static bool IsNullOrEmpty(string value) 指示指定的字符串是null还是Empty字符串。
    public static string Join(string separator, params string[] value) 使用每个元素之间的指定分隔符来连接字符串数组的所有元素。
    public static string Join(string separator, string[] value, int startIndex, int count) 使用每个元素之间的指定分隔符来连接字符串数组的指定元素。
    public int LastIndexOf(char value) 返回当前字符串对象中最后一次出现指定Unicode字符的从零开始的索引位置。
    public int LastIndexOf(string value) 返回指定字符串在当前字符串对象中最后一次出现的从零开始的索引位置。
    public string Remove(int startIndex) 删除当前实例中从指定位置开始到最后一个位置的所有字符,并返回字符串。
    public string Remove(int startIndex, int count) 从指定位置开始删除当前字符串中指定数量的字符,然后返回该字符串。
    public string Replace(char oldChar, char newChar) 用指定的Unicode字符替换当前字符串对象中所有出现的指定Unicode字符,并返回新字符串。
    public string Replace(string oldValue, string newValue) 用指定的字符串替换当前字符串对象中所有出现的指定字符串,并返回新的字符串。
    public string[] Split(params char[] separator) 返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的Unicode字符数组的元素分隔。
    public string[] Split(char[] separator, int count) 返回一个字符串数组,其中包含当前字符串对象中的子字符串,由指定的Unicode字符数组的元素分隔。 int参数指定要返回的最大子字符串数。
    public bool StartsWith(string value) 确定此字符串实例的开头是否与指定的字符串匹配。
    public char[] ToCharArray() 返回一个Unicode字符数组,其中包含当前字符串对象中的所有字符。
    public char[] ToCharArray(int startIndex, int length) 返回一个Unicode字符数组,其中包含当前字符串对象中的所有字符,从指定的索引开始,直至指定的长度。
    public string ToLower() 返回此字符串的副本,转换为小写形式。
    public string ToUpper() 返回此字符串的副本,转换为大写。
    public string Trim() 从当前String对象中删除所有前导和尾随空格字符。
    您可以访问MSDN库以获取方法和String类构造函数的完整列表。
  • 例子

    以下示例演示了上述某些方法-
    比较字符串
        
    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
    
上一节:
下一节: