首页  >  文章  >  后端开发  >  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删除