C# 字符串

2018-09-27 16:30 更新

字符串

在 C# 中,可以使用字符串作為字符數(shù)組,但更常見(jiàn)的做法是使用 string 關(guān)鍵字來(lái)聲明一個(gè)字符串變量。該 string 關(guān)鍵字是 System.String 類(lèi)的別名。

創(chuàng)建一個(gè) String 對(duì)象

可以使用下列方法之一字符串對(duì)象:

  • 通過(guò)指定一個(gè)字符串給一個(gè)字符串變量
  • 通過(guò)使用 String 類(lèi)的構(gòu)造函數(shù)
  • 通過(guò)使用字符串連接運(yùn)算符(+)
  • 通過(guò)檢索屬性或調(diào)用返回一個(gè)字符串的方法
  • 通過(guò)調(diào)用格式化方法的值或?qū)ο筠D(zhuǎn)換成它的字符串表示

下面的例子說(shuō)明了這一點(diǎn):

using System;
namespace StringApplication
{
   class Program
   {
      static void Main(string[] args)
      {
         //from string literal and string concatenation
         string fname, lname;
         fname = "Rowan";
         lname = "Atkinson";

         string fullname = fname + lname;
         Console.WriteLine("Full Name: {0}", fullname);

         //by using string constructor
         char[] letters = { 'H', 'e', 'l', 'l','o' };
         string greetings = new string(letters);
         Console.WriteLine("Greetings: {0}", greetings);

         //methods returning string
         string[] sarray = { "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);
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

Full Name: Rowan Atkinson
Greetings: Hello
Message: Hello From Tutorials Point
Message: Message sent at 5:58 PM on Wednesday, October 10, 2012

String 類(lèi)的屬性

String 類(lèi)有以下兩個(gè)屬性:

序號(hào)屬性名稱(chēng)和描述
1Chars 獲取在當(dāng)前字符串對(duì)象中的指定位置的字符對(duì)象
2Length 獲取字符在當(dāng)前字符串對(duì)象的數(shù)目

String 類(lèi)的方法

String 類(lèi)有許多方法,以幫助使用 String 對(duì)象。下表提供了一些最常用的方法:

序號(hào)屬性名稱(chēng)和描述
1public static int Compare(string strA,string strB)
比較兩個(gè)指定的字符串對(duì)象,并返回一個(gè)整數(shù),指示其在排序順序相對(duì)位置
2public static int Compare(string strA,string strB,bool ignoreCase)
比較兩個(gè)指定的字符串對(duì)象,并返回一個(gè)整數(shù),指示其在排序順序相對(duì)位置。但是,它忽略情況下,如果布爾參數(shù)為true
3public static string Concat(string str0,string str1)
連接兩個(gè)字符串對(duì)象
4public static string Concat(string str0,string str1,string str2)
拼接三個(gè)字符串對(duì)象
5public static string Concat(string str0,string str1,string str2,string str3)
符連接四個(gè)字符串對(duì)象
6public bool Contains(string value)
返回一個(gè)值,指示指定的字符串對(duì)象是否發(fā)生此字符串中
7public static string Copy(string str)
創(chuàng)建具有相同的值作為指定字符串的新String對(duì)象
8public void CopyTo(int sourceIndex,char[] destination,int destinationIndex,int count)
復(fù)制從字符串對(duì)象到指定位置Unicode字符數(shù)組的指定位置指定的字符數(shù)
9public bool EndsWith(string value)
確定字符串對(duì)象的末尾是否與指定的字符串匹配
10public bool Equals(string value)
確定當(dāng)前字符串對(duì)象,并指定字符串對(duì)象是否具有相同的值
11public static bool Equals(string a,string b)
確定兩個(gè)指定的String對(duì)象是否具有相同的值
12public static string Format(string format,Object arg0)
替換指定的字符串指定對(duì)象的字符串表示在一個(gè)或多個(gè)格式項(xiàng)
13public int IndexOf(char value)
返回當(dāng)前字符串指定Unicode字符中第一次出現(xiàn)從零開(kāi)始的索引
14public int IndexOf(string value)
返回在這種情況下指定字符串中第一次出現(xiàn)從零開(kāi)始的索引
15public int IndexOf(char value,int startIndex)
返回此字符串指定Unicode字符中第一次出現(xiàn)從零開(kāi)始的索引,搜索開(kāi)始在指定的字符位置
16public int IndexOf(string value,int startIndex)
返回在這種情況下指定字符串中第一次出現(xiàn)的從零開(kāi)始的索引,搜索開(kāi)始在指定的字符位置
17public int IndexOfAny(char[] anyOf)
返回Unicode字符指定數(shù)組中第一次出現(xiàn)的任何字符的這個(gè)實(shí)例從零開(kāi)始的索引
18public int IndexOfAny(char[] anyOf,int startIndex)
返回Unicode字符指定數(shù)組,開(kāi)始搜索從指定字符位置中第一次出現(xiàn)的任何字符的這個(gè)實(shí)例從零開(kāi)始的索引
19public string Insert(int startIndex,string value)
返回在指定的字符串被插入在當(dāng)前字符串對(duì)象指定索引位置一個(gè)新的字符串
20public static bool IsNullOrEmpty(string value)
指示指定的字符串是否為空或空字符串
21public static string Join(string separator,params string[] value)
連接字符串?dāng)?shù)組中的所有元素,使用每個(gè)元件之間指定的分隔
22public static string Join(string separator,string[] value,int startIndex,int count)
連接字符串?dāng)?shù)組的指定元素,利用每一個(gè)元素之間指定分隔符
23public int LastIndexOf(char value)
返回當(dāng)前字符串對(duì)象中指定的Unicode字符的最后出現(xiàn)從零開(kāi)始的索引位置
24public int LastIndexOf(string value)
返回當(dāng)前字符串對(duì)象中的指定字符串最后一次出現(xiàn)的從零開(kāi)始的索引位置
25public string Remove(int startIndex)
刪除在當(dāng)前實(shí)例中的所有字符,開(kāi)始在指定的位置,并繼續(xù)通過(guò)最后位置,并返回字符串
26public string Remove(int startIndex,int count)
刪除在當(dāng)前字符串的字符開(kāi)始的指定位置的指定數(shù)量,并返回字符串
27public string Replace(char oldChar,char newChar)
替換與指定的Unicode字符當(dāng)前字符串對(duì)象指定的Unicode字符的所有匹配,并返回新的字符串
28public string Replace(string oldValue,string newValue)
替換用指定的字符串當(dāng)前字符串對(duì)象指定的字符串的所有匹配,并返回新的字符串
29public string[] Split(params char[] separator)
返回一個(gè)字符串?dāng)?shù)組,其中包含的子字符串在當(dāng)前字符串對(duì)象,由指定的Unicode字符數(shù)組的元素分隔
30public string[] Split(char[] separator,int count)
返回一個(gè)字符串?dāng)?shù)組,其中包含的子字符串在當(dāng)前字符串對(duì)象,由指定的Unicode字符數(shù)組的元素分隔。整型參數(shù)指定的子串返回最大數(shù)量
31public bool StartsWith(string value)
確定此字符串實(shí)例的開(kāi)頭是否與指定的字符串匹配
32public char[] ToCharArray()
返回一個(gè)Unicode字符數(shù)組,在當(dāng)前字符串對(duì)象中的所有字符
33public char[] ToCharArray(int startIndex,int length)
返回一個(gè)Unicode字符數(shù)組,在當(dāng)前字符串對(duì)象中的所有字符,從指定的索引開(kāi)始,并到指定的長(zhǎng)度
34public string ToLower()
返回此字符串的一個(gè)副本轉(zhuǎn)換為小寫(xiě)
35public string ToUpper()
返回此字符串的一個(gè)副本轉(zhuǎn)換為大寫(xiě)
36public string Trim()
從當(dāng)前String對(duì)象去除所有開(kāi)頭和結(jié)尾的空白字符

方法上述名單并不是詳盡的信息,請(qǐng)?jiān)L問(wèn)MSDN庫(kù)的方法和String類(lèi)的構(gòu)造函數(shù)的完整列表。

示例:

下面的例子說(shuō)明了一些上面提到的方法:

比較字符串:

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() ;
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

This is test and This is text are not equal.

String包含字符串:

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() ;
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

The sequence 'test' was found.

獲取一個(gè)子字符串:

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);
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

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);
      }
   }
}

編譯執(zhí)行上述代碼,得到如下結(jié)果:

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
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)