06.string.md 7.7 KB

字符串

string 类型是一种特殊的引用类型,每次操作都会生成一个新的副本进行操作。

属性

Length

获取当前字符串中字符的个数

string str = "hello world";
int len = str.Length; 

方法

Compare

静态方法,比较两个字符串的先后,返回值小于零,说明字符串A顺序在字符串B前面;返回零,说明两者顺序相同;大于零,说明字符串A顺序在字符串B后面

常用重载

  • Compare(String? strA, String? strB)

  • Compare(String? strA, String? strB, bool ignoreCase) //是否忽略大小写

  • Compare(String? strA, String? strB, bool ignoreCase, CultureInfo? culture) //指定区域信息

string strA = "001";
string strB = "002";
int result = String.Compare(strA, strB);

Concat

静态方法,连接指定的字符串,并返回拼接后的字符串

常用重载

  • Concat(params String?[] values)
  • Concat(String? str0, String? str1)
string[] strs = new string[]{"a","b","c","d"};
string strNew = String.Concat(strs);

Copy

静态方法,复制指定的字符串

String.Copy("abcde")

Equals

静态方法,判断两个字符串是否相等

常用重载

  • Equals(String? a, String? b)
  • Equals(String? a, String? b, StringComparison comparisonType)用于指定比较用的排序规则和是否区分大小写
string str1 = "hello world";
string str2 = "Hello World";
if(string.Equals(str1, str2)
{
    ……
}

Join

静态方法,用指定的字符或字符串,连接多个字符串

常用重载

  • Join(String? separator, IEnumerable values)
  • Join(String? separator, params String?[] value)
  • Join(char separator, params String?[] value)
  • string[] strs = new string[] { "a", "b", "c", "d" };
    string strNew = string.Join(',',strs);
    

    CompareTo

    用于和指定的字符串做比较

    string str1 = "Hello";
    string str2 = "hello";
    int result = str1.CompareTo(str2);
    

    Contains

    判断字符串是否包含指定字符或字符串

    常用重载

    • Contains(String value)
    • Contains(char value)
    string str = "hello world";
    bool result = str.Contains('o');
    

    EndsWith

    判断字符串是否以指定的字符或字符串结尾

    常用重载

    • EndsWith(char value)
    • EndsWith(String value)
    • EndsWith(String value, bool ignoreCase, CultureInfo? culture)
    • EndsWith(String value, StringComparison comparisonType)
    string str = "hello world";
    bool result = str.EndWith('o');
    

    Equals

    判断字符串是否与指定的字符串相等

    常用重载

    • Equals(String? value)
    • Equals(String? value, StringComparison comparisonType)
    string str = "hello world";
    bool result = str.Equals("HELLO WORLD");
    

    IndexOf

    获取指定字符或字符串在字符串中首次出现的位置

    常用重载

    • IndexOf(String value, StringComparison comparisonType)
    • IndexOf(String value, int startIndex, StringComparison comparisonType)
    • IndexOf(String value, int startIndex, int count, StringComparison comparisonType)
    • IndexOf(String value, int startIndex, int count)
    • IndexOf(String value, int startIndex)
    • IndexOf(String value)
    • IndexOf(char value, StringComparison comparisonType)
    • IndexOf(char value, int startIndex, int count)
    • IndexOf(char value, int startIndex)
    • IndexOf(char value)
    string str = "hello world";
    int result = str.Indexof('o');
    

    LastIndexOf

    获取指定字符或字符串在字符串中最后一次出现的位置

    常用重载

    • LastIndexOf(char value, int startIndex, int count)
    • LastIndexOf(char value, int startIndex)
    • LastIndexOf(String value, StringComparison comparisonType)
    • LastIndexOf(String value, int startIndex, StringComparison comparisonType)
    • LastIndexOf(char value)
    • LastIndexOf(String value, int startIndex, int count)
    • LastIndexOf(String value, int startIndex)
    • LastIndexOf(String value)
    • LastIndexOf(String value, int startIndex, int count, StringComparison comparisonType)
    • LastIndexOfAny(char[] anyOf)
    • LastIndexOfAny(char[] anyOf, int startIndex)
    • LastIndexOfAny(char[] anyOf, int startIndex, int count)
    string str = "hello world";
    int result = str.LastIndexOf('o');
    

    PadLeft

    把字符串从左侧填充到指定的长度

    常用重载

    • PadLeft(int totalWidth) 用空格填充
    • PadLeft(int totalWidth, char paddingChar) 用指定字符串填充
    string str = "hello";
    string str1 = str.PadLeft(10);
    

    PadRight

    把字符串从右侧填充到指定的长度

    常用重载

    • PadRight(int totalWidth) 用空格填充
    • PadRight(int totalWidth, char paddingChar) 用指定字符串填充
    string str = "hello";
    string str1 = str.PadRight(10);
    

    Remove

    在字符串中从指定位置移除指定长度的字符串

    常用重载

    • Remove(int startIndex) 指定位置后面的字符串全部移除
    • Remove(int startIndex, int count)
    string str = "hello";
    string str1 = str.Remove(2,1);
    

    Replace

    用指定的新字符串替换指定的老字符串

    常用重载

    • Replace(char oldChar, char newChar)
    • Replace(String oldValue, String? newValue)
    • Replace(String oldValue, String? newValue, bool ignoreCase, CultureInfo? culture)
    • Replace(String oldValue, String? newValue, StringComparison comparisonType)
    string str1 = "Hello World";
    string str2 = str1.Replace("World","China");
    

    Split

    按照指定的字符将字符串拆分为数组

    常用重载

    • Split(String[]? separator, int count, StringSplitOptions options)
    • Split(String? separator, int count, StringSplitOptions options = StringSplitOptions.None)
    • Split(String[]? separator, StringSplitOptions options)
    • Split(char[]? separator, int count, StringSplitOptions options)
    • Split(String? separator, StringSplitOptions options = StringSplitOptions.None)
    • Split(char[]? separator, StringSplitOptions options)
    • Split(char separator, StringSplitOptions options = StringSplitOptions.None)
    • Split(params char[]? separator)
    • Split(char[]? separator, int count)、
    • Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None)
    string str = "Hello World";
    string[] strs = str.Split('');
    

    StartsWith

    判断字符串是否已指定的字符或字符串开头

    常用重载

    • StartsWith(char value)
    • StartsWith(String value)
    • StartsWith(String value, bool ignoreCase, CultureInfo? culture)
    • StartsWith(String value, StringComparison comparisonType)
    string str = "Hello World";
    bool result = str.StartsWith('h');
    

    Substring

    从制定位置截取指定长度的字符串

    常用重载

    • Substring(int startIndex)
    • Substring(int startIndex, int length)
    string str = "Hello World";
    string result = str.Substring(2,2);
    

    ToLower

    将字符串转换成小写形式

    常用重载

    • ToLower()
    • ToLower(CultureInfo? culture),用于指定区域性信息
    string str = "HELLO WORLD";
    string strNew = str.ToLower();
    

    ToUpper

    将字符串转化成大写形式

    常用重载

    • ToUpper()
    • ToUpper(CultureInfo? culture),用于指定区域性信息
    string str = "hello world";
    string strNew = str.ToUpper();
    

    Trim

    去掉字符串两端指定的字符

    常用重载

    • Trim()
    • Trim(char trimChar)
    • Trim(params char[]? trimChars)
    string str = "  hello world  ";
    string strNew = str.Trim();
    

    TrimEnd

    去掉字符串右端指定的字符

    常用重载

    • TrimEnd()
    • TrimEnd(char trimChar)
    • TrimEnd(params char[]? trimChars)
    string str = "  hello world  ";
    string strNew = str.TrimEnd();
    

    TrimStart

    去掉字符串左端指定的字符

    常用重载

    • TrimStart()
    • TrimStart(char trimChar)
    • TrimStart(params char[]? trimChars)
    string str = "  hello world  ";
    string strNew = str.TrimStart();