在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ C#字符串
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關(guān)鍵字
C# StreamReader類(lèi)
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類(lèi)
C#主線程
C#數(shù)據(jù)類(lèi)型
C# FileStream類(lèi)
C#預(yù)處理指令
C#繼承
C#循環(huán)
C#決策結(jié)構(gòu)
C#集合
C#反射
C#類(lèi)型轉(zhuǎn)換
C#泛型
C# StringReader類(lèi)
C#歷史
C#運(yùn)算符重載
C#屬性
C#線程實(shí)例:Sleep()方法
C#線程示例:優(yōu)先級(jí)
C#線程實(shí)例:Join()方法
C# BinaryReader類(lèi)
C#類(lèi)
C#索引器
C# BinaryWriter類(lèi)
C#序列化
C#常量和文字
C#程序結(jié)構(gòu)
C#封裝
C#事件
C#可空類(lèi)型(nullable)
C#基本語(yǔ)法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實(shí)例
C# StringWriter類(lèi)
C#線程實(shí)例
C#數(shù)組
C#正則表達(dá)式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類(lèi)
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類(lèi)
C#線程實(shí)例:Abort()方法
C#結(jié)構(gòu)體
C#運(yùn)算符
C#入門(mén)程序
C#多線程生命周期
C# TextReader類(lèi)
C# DirectoryInfo類(lèi)
C#委托

C#字符串

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

創(chuàng)建一個(gè)字符串對(duì)象

可以使用以下方法之一來(lái)創(chuàng)建字符串對(duì)象:

  • 通過(guò)將一個(gè)字符串文字分配給一個(gè)String變量
  • 通過(guò)使用String類(lèi)構(gòu)造函數(shù)
  • 通過(guò)使用字符串連接運(yùn)算符(+)
  • 通過(guò)檢索屬性或調(diào)用返回字符串的方法
  • 通過(guò)調(diào)用格式化方法將值或?qū)ο筠D(zhuǎ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", "Yiibai", "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);
      }
   }
}

當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:

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

String類(lèi)的屬性

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

編號(hào) 屬性 說(shuō)明
1 Chars 獲取當(dāng)前String對(duì)象中指定位置的Char對(duì)象。
2 Length 獲取當(dāng)前String對(duì)象中的字符數(shù)。

String類(lèi)的方法

String類(lèi)有許多方法可以幫助我們來(lái)處理字符串對(duì)象。下表中提供了一些最常用的方法:

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

您可以訪問(wèn)MSDN庫(kù),獲取方法和String類(lèi)構(gòu)造函數(shù)的完整列表。

實(shí)例

以下示例演示了上述一些方法:

比較字符串:

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

當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:

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

當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:

The sequence 'test' was found.

獲取子字符串:

using System;
namespace StringApplication
{
   class StringProg
   {
      static void Main(string[] args)
      {
         string str = "Last night I dreamt of Girls";
         Console.WriteLine(str);
         string substr = str.Substring(23);
         Console.WriteLine(substr);
      }
   }
}

當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(jié)果:

Girls

將字符串連接:

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

當(dāng)編譯和執(zhí)行上述代碼時(shí),會(huì)產(chǎn)生以下結(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

上一篇:C#委托下一篇:C#線程命名實(shí)例