首頁  >  文章  >  後端開發  >  C# 字串方法

C# 字串方法

王林
王林轉載
2023-09-11 09:21:041104瀏覽

C# 字符串方法

String 類別有許多方法可以幫助您處理字串物件。下表列出了一些最常用的方法-

Sr.No 方法及說明
1 public static int Compare(string strA, string strB)

比較兩個指定的字串物件並傳回一個整數,指示它們在排序順序中的相對位置。

2 public static int Compare(string strA, string strB, boolignoreCase) strong>

#比較兩個指定的字串物件並傳回一個整數,指示它們在排序順序中的相對位置。但是,如果布林參數為 true,則忽略大小寫。

3 public static string Concat(string str0, string str1)

#連接兩個字串物件。

4 public static string Concat(string str0, string str1, string str2)

#連接三個字串物件。

5 公共靜態字串Concat(字串str0,字串str1,字串str2,字串str3)

連接四個字串物件。

6 public bool Contains(string value)

傳回一個值,指示指定的String 物件是否出現在該字串中。

7 public static string Copy(string str)

建立一個與指定值相同的新String 物件字串。

8 public void CopyTo(int sourceIndex, char[] 目的地, int destinationIndex, int count)

將指定數量的字元從String 物件的指定位置複製到Unicode 字元陣列中的指定位置。

9 public bool EndsWith(string value)

判斷是否字串物件的末尾與指定字串匹配。

10 public bool equals(string value)

判斷目前String物件與指定String物件是否具有相同的值。

11 public static bool Equals(string a, string b)

判斷兩個指定的String物件是否相同

讓我們來看一個在C# 中使用Contains() 方法的範例。 Contains(string value) 傳回一個值,指示指定的 String 物件是否出現在此字串中。

範例

using System;
namespace Demo {

   class Program {

      static void Main(string[] args) {
         string str = "This is test";
     
         if (str.Contains("test")) {
            Console.WriteLine("Yes, 'test' was found.");
         }
         Console.ReadKey() ;
      }
   }
}
#

以上是C# 字串方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除