数据类型--静态字符串string

1.比较字符串

  (1) Compare方法(String类的静态方法)

  ① int Compare ( string strA , string strB )

  ② int Compare ( string strA , string strB , bool ignoreCase )

  ③ int Compare ( string strA , string strB , StringComparison comparisonType )

  ④ int Compare ( string strA , string strB , bool ignoreCase , CultureInfo culture)

  ⑤ int Compare ( string strA , int indexA , string strB , int indexB , int length )

  ⑥ int Compare ( string strA , int indexA , string strB , int IndexB , int length , bool ignoreCase )

  ⑦ int Compare ( string strA , int , string strB , int indexA , int length , StringComparison comparisonType )

  ⑧ int Compare ( string strA , int , string strB , int indexA , int length , bool ignoreCase , CultureInfo culture )

  ignoreCase:是否考虑大小写,不考虑(true),考虑(false)

  comparisonType:指示比较是应使用当前区域还是固定区域性,是应考虑还是应忽略比较子的大小写,是使用字排序规则(区分区域性)还是序号排序规则(不区分区域性)。其取值为StringComparison枚举,如下所示:

  ▲ CurrentCulture:使用区域敏感排序规则和当前区域比较字符串

  ▲ CurrentCultureIgnoreCase:使用区域敏感排序规则、当前区域来比较字符串,同时忽略被比较字符串的大小写

  ▲ InvariantCulture:使用区域敏感排序规则和固定区域比较字符串

  ▲ InvariantCultureIgnoreCase:使用区域敏感排序规则、固定区域来比较字符串,同时忽略被比较字符串的大小写

  ▲ Ordinal:使用序号排序规则比较字符串

  ▲ OrdinalIgnoreCase:使用序号排序规则并忽略被比较字符串的大小写对字符串进行比较

  index:在当需要比较两个字符串中的字串是,index为str中子字符串的起始位置

  length:待比较字符串的最大长度

  culture:字符串的区域信息,指示字符串的区域性特定信息

  如果strA大于strB,返回正整数,等于返回0,小于返回负整数。

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  bool b = String.Compare(strA , strB);  //注意是String不是string

  ---eg-->

  PS:String类的另一个比较方法CompareOrdinal同Compare方法非常相似,它比较两个字符串,但不考虑区域性问题。

  (2) CompareTo方法(不是静态方法)

  CompareTo没有重载形式,只能按照大小写敏感方式比较两个字符串,返回值和Compare相同。

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  bool b = strA.CompareTo(strB);

  ---eg-->

  (3) Equals方法(String类的静态方法和非静态方法)

  ① bool Equals ( string )

  ② bool Equals ( string , StringComparison comparisonType)

  ③ static bool Equals ( string , string )

  ④ static bool Equals ( string , string , StringComparison comparisonType )

  如果两个字符串相等,返回true,否则,返回false。

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  bool b1 = strA.Equals(strB);  //return false

  bool b2 = String.Equals(strA , strB);  //return false

  ---eg-->

  (4) 比较运算符方法

  String支持两个比较运算符“==“和”!=“。

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  bool b1 = (strA == strB);  //return false

  bool b2 = (strA != strB);  //return true

  ---eg-->

  2.判断首尾字符串

  <--eg---

  string str = “Hello”;

  bool b1 = str.StartsWith(“He”);  //return true

  bool b2 = str.EndsWith(“ol”);  //return false

  ---eg-->

  3.判断是否包含子串

  <--eg---

  string str = “Hello”;

  bool b = str.Contains(“He”);  //return true

  ---eg-->

  4.定位字符和子串

  定位子串是指在一个字符串中寻找其中包含的字串或者某个字符。

  (1) IndexOf / LastIndexOf

  IndexOf方法用于搜索在一个字符串中,某个特定的字符或者子串或者子串第一次出现的位置,该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。

  ① int IndexOf ( char / string value )

  ② int IndexOf ( char / string value , int startIndex  )

  ③ int IndexOf ( char / string value , int startIndex , int count )

  value:待定位的字符或者子串

  startIndex:在总串中开始搜索的字符数

  count:在总串中从起始位置开始搜索的字符数

  <--eg---

  string str = “Hello”;

  int i = str.IndexOf(‘l’);  //return 2

  ---eg-->

  LastIndexOf用于搜索在一个字符串中,某个特定的字符或者子串或者子串最后一次出现的位置,其方法定义和返回值都与IndexOf相同。

 

(2) IndexOfAny / LastIndexOfAny

  IndexOfAny方法功能与IndexOf类似,区别在于,它可以搜索在一个字符串中,出现在一个字符数组中的任意字符第一次出现的位置。该方法区分大小写,并从字符串的首字符开始以0计数。如果字符串中不包含这个字符或子串,则返回-1。

  ① int IndexOfAny / LastIndexOfAny ( char[] anfOf )

  ② int IndexOfAny / LastIndexOfAny ( char[] anfOf , int startIndex  )

  ③ int IndexOfAny/LastIndexOfAny(char[] anfOf , int startIndex , int count)

  anfOf:待定位的字符数组

  startIndex:在原字符串中开始搜索的字符数

  count:在原字符串中从起始位置开始搜索的字符数

  <--eg---

  string str = “Hello”;

  char[] anyOf = { ‘H’ , ‘e’ , ‘l’ };

  int i1 = str.IndexOfAny(anyOf);  //return 0

  int i2 = str.LastIndexOfAny(anyOf);  //return 3

  ---eg-->

  LastIndexOfAny用于搜索在一个字符串中,出现在一个字符数组中的任意字符最后一次出现的位置,其方法定义和返回值都与IndexOfAny相同。

  5.格式化字符串

  Format方法用于创建格式化的字符串及连接多个字符串对象。

  static string Format ( string format , parm object[] args );

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  string str = String.Format ( “{0}...{1}” , strA , strB ); //Hello...World

  ---eg-->

  6.连接字符串(String类的静态方法)

  (1) Concat方法

  Concat方法用于连接两个或多个字符串。

  static string Concat ( param string[] values)

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  string str = String.Concat ( strA , ”,” , strB ); //Hello,World

  ---eg-->

  (2) Join方法

  Join方法利用一个字符数组和一个分隔符构造新的字符串。

  static string Join ( string separator , string[] value );

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  string strC = { strA , strB };

  string str = String.Join (”,” , strC ); //Hello,World

  ---eg-->

  (3) 连接运算符“+”

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  string str = strA + strB; //Hello,World

  ---eg-->

  7.拆分字符串

  (1) 根据字符拆分

  public string[] Split ( param char[] separator , StringSplitOptions.Option );

  Option:为RemoveEmptyEntries时表示不包括空字符串,为None时表示包括。可选参数。

  <--eg---

  string str = “Hello World”;

  char[] s = {‘ ’};  //分隔符

  string[] Newstr = str.Split( s , StringSplitOptions. RemoveEmptyEntries );

  for ( int i=0 ; i < Newstr.Count() ; i++ )  //或者Newstr.Length

  {

  Console.WriteLine( Newstr[i] );

  }

  ---eg-->

  (2) 根据字符串拆分

  public string[] Split ( string[] separator , StringSplitOptions.Option );

  Option:为RemoveEmptyEntries时表示不包括空字符串,为None时表示包括。可选参数。

  <--eg---

  string str = “Hello...World”;

  string[] s = {“...”};  //分隔符

  string[] Newstr = str.Split( s , StringSplitOptions. RemoveEmptyEntries );

  for ( int i=0 ; i < Newstr.Count() ; i++ )  //或者Newstr.Length

  {

  Console.WriteLine( Newstr[i] );

  }

  ---eg-->

8.插入字符串

  public string Insert ( int startIndex , string value );

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  string str = strA.Insert( 5 , strB );  // return HelloWorld

  ---eg-->

  9.填充字符串

  public string PadLeft / PadRight ( int totalWidth );  //填充空格

  public string PadLeft / PadRight ( int totalWidth , char paddingChar );

  <--eg---

  string strA = “Hello”;

  string strB = strA.PadLeft ( 5 , ‘#’ );  // return #####Hello

  string strC = strA.PadRight( 5 , ‘#’ );  // return Hello#####

  ---eg-->

  10.删除字符串

  (1) Remove方法

  public string Remove ( int startIndex , int count );

  <--eg---

  string strA = “Hello”;

  string str = strA.Remove ( 1 , 3 );  // return Ho

  ---eg-->

  (2) Trim / TrimStart / TrimEnd方法

  这个方法可以把一个字符串首尾处的一些特殊字符剪掉。

  public string Trim ( );  //默认删除空格

  public string Trim ( params char[] trimChars );

  <--eg---

  string strA = “@Hello$&*”;

  char[] c = { '@' , '#' , '$' , '&' , '*' };

  string str = strA.Trim ( c );  // return Hello

  ---eg-->

  TrimStart和TrimEnd分别剪掉一个字符串开头和结尾的特殊字符。

  11.复制字符串

  (1) Copy方法(String类的静态方法)

  static string Copy ( string str );

  <--eg---

  string strA = “Hello”;

  string strB = “World”;

  strB = String.Copy ( strA );  // return Hello

  ---eg-->

  (2) CopyTo方法

  public void CopyTo

  ( int sourceIndex , char[] destination , int destinationIndex , int count );

  sourceIndex:需要复制的字符起始位置

  destination:目标字符数组

  destinationIndex: 目标字符数组开始存放位置

  count:所要复制的字符个数

  12.替换字符串

  public string Replace ( char oldChar , char newChar );

  public string Replace ( string oldString , string newString );

  <--eg---

  string strA = “Hello”;

  string str = strA.Replace (“ll”,”r”);  // return Hero

  ---eg-->

  13.更改大小写

  <--eg---

  string str = “Hello”;

  string strU = str.ToUpper ();  // return HELLO

  string strD = str.ToLower ();  // return hello

  ---eg-->

  14.获取子字符串

  public string SubString ( int startIndex , int Length )

  <--eg---

  string strA = “Hello”;

  string str = strA.Substring ( 0 , 2 );  // return He

  ---eg-->

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值