c#string 方法

来源:互联网 发布:linux vi怎么保存退出 编辑:程序博客网 时间:2024/06/05 08:15

string类型表示一个字符序列(零个或更多 Unicode 字符)

尽管 string 是引用类型,但定义相等运算符(== 和 !=)是为了比较 string 对象(而不是引用)的值


使用string类时,一旦创建了对象,就不能够修改。string的每次修改实际上都是创建了一个新的对象,然后赋值给所用了引用,如果要真正的修改内容,可以使用StringBuilder类。


string是System.String的别名,两个完全相等,可以从定义里看到。

    public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>

string的常用操作:

一、比较

1、Compare

比较字符串在英文字典中的位置,在引文字典中,前面的单词小于后面的单词。它是静态方法,可以直接调用;

常用两种重载形式如下:

public static int Compare(string strA, string strB);public static int Compare(string strA, string strB, bool ignoreCase);

比如:string.Compare("abc", "abd") ,结果为 -1,因为c的ASCⅡ码小于d

定义如下:

        //        // 摘要:        //     比较两个指定的 System.String 对象,并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要比较的第一个字符串。        //        //   strB:        //     要比较的第二个字符串。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 小于 strB。零strA 等于 strB。大于零strA 大于 strB。        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public static int Compare(string strA, string strB);        //        // 摘要:        //     比较两个指定的 System.String 对象(其中忽略或考虑其大小写),并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要比较的第一个字符串。        //        //   strB:        //     要比较的第二个字符串。        //        //   ignoreCase:        //     要在比较过程中忽略大小写,则为 true;否则为 false。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 小于 strB。零strA 等于 strB。大于零strA 大于 strB。        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public static int Compare(string strA, string strB, bool ignoreCase);        //        // 摘要:        //     使用指定的规则比较两个指定的 System.String 对象,并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要比较的第一个字符串。        //        //   strB:        //     要比较的第二个字符串。        //        //   comparisonType:        //     一个枚举值,用于指定比较中要使用的规则。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 小于 strB。零strA 等于 strB。大于零strA 大于 strB。        //        // 异常:        //   System.ArgumentException:        //     comparisonType 不是一个 System.StringComparison 值。        //        //   System.NotSupportedException:        //     不支持 System.StringComparison。        [SecuritySafeCritical]        public static int Compare(string strA, string strB, StringComparison comparisonType);        //        // 摘要:        //     比较两个指定的 System.String 对象(其中忽略或考虑其大小写,并使用区域性特定的信息干预比较),并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要比较的第一个字符串。        //        //   strB:        //     要比较的第二个字符串。        //        //   ignoreCase:        //     要在比较过程中忽略大小写,则为 true;否则为 false。        //        //   culture:        //     一个对象,提供区域性特定的比较信息。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 小于 strB。零strA 等于 strB。大于零strA 大于 strB。        //        // 异常:        //   System.ArgumentNullException:        //     culture 为 null。        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture);        //        // 摘要:        //     对两个指定的 System.String 对象进行比较,使用指定的比较选项和区域性特定的信息来影响比较,并返回一个整数,该整数指示这两个字符串在排序顺序中的关系。        //        // 参数:        //   strA:        //     要比较的第一个字符串。        //        //   strB:        //     要比较的第二个字符串。        //        //   culture:        //     提供区域性特定的比较信息的区域性。        //        //   options:        //     要在执行比较时使用的选项(如忽略大小写或符号)。        //        // 返回结果:        //     一个 32 位有符号整数,该整数指示 strA 与 strB 之间的词法关系,如下表所示值条件小于零strA 小于 strB。零strA 等于 strB。大于零strA        //     大于 strB。        //        // 异常:        //   System.ArgumentException:        //     options 不是一个 System.Globalization.CompareOptions 值。        //        //   System.ArgumentNullException:        //     culture 为 null。        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options);        //        // 摘要:        //     对两个指定的 System.String 对象的子字符串进行比较,并返回一个指示二者在排序顺序中的相对位置的整数。        //        // 参数:        //   strA:        //     要在比较中使用的第一个字符串。        //        //   indexA:        //     strA 中子字符串的位置。        //        //   strB:        //     要在比较中使用的第二个字符串。        //        //   indexB:        //     strB 中子字符串的位置。        //        //   length:        //     要比较的子字符串中字符的最大数量。        //        // 返回结果:        //     一个 32 位有符号整数,指示两个比较数之间的词法关系。值条件小于零strA 中的子字符串小于 strB 中的子字符串。零子字符串相等,或者 length        //     为零。大于零strA 中的子字符串大于 strB 中的子字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     indexA 大于 strA.System.String.Length。- 或 -indexB 大于 strB.System.String.Length。-        //     或 -indexA、indexB 或 length 为负。- 或 -indexA 或 indexB 为 null,而 length 大于零。        public static int Compare(string strA, int indexA, string strB, int indexB, int length);        //        // 摘要:        //     比较两个指定的 System.String 对象的子字符串(忽略或考虑其大小写),并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要在比较中使用的第一个字符串。        //        //   indexA:        //     strA 中子字符串的位置。        //        //   strB:        //     要在比较中使用的第二个字符串。        //        //   indexB:        //     strB 中子字符串的位置。        //        //   length:        //     要比较的子字符串中字符的最大数量。        //        //   ignoreCase:        //     要在比较过程中忽略大小写,则为 true;否则为 false。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 中的子字符串小于 strB 中的子字符串。零子字符串相等,或者 length        //     为零。大于零strA 中的子字符串大于 strB 中的子字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     indexA 大于 strA.System.String.Length。- 或 -indexB 大于 strB.System.String.Length。-        //     或 -indexA、indexB 或 length 为负。- 或 -indexA 或 indexB 为 null,而 length 大于零。        public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase);        //        // 摘要:        //     使用指定的规则比较两个指定的 System.String 对象的子字符串,并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要在比较中使用的第一个字符串。        //        //   indexA:        //     strA 中子字符串的位置。        //        //   strB:        //     要在比较中使用的第二个字符串。        //        //   indexB:        //     strB 中子字符串的位置。        //        //   length:        //     要比较的子字符串中字符的最大数量。        //        //   comparisonType:        //     一个枚举值,用于指定比较中要使用的规则。        //        // 返回结果:        //     一个 32 位带符号整数,指示两个比较数之间的词法关系。值条件小于零strA 参数中的子字符串小于 strB 参数中的子字符串。零子字符串相等,或者        //     length 参数为零。大于零strA 中的子字符串大于 strB 中的子字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     indexA 大于 strA.System.String.Length。- 或 -indexB 大于 strB.System.String.Length。-        //     或 -indexA、indexB 或 length 为负。- 或 -indexA 或 indexB 为 null,而 length 大于零。        //        //   System.ArgumentException:        //     comparisonType 不是一个 System.StringComparison 值。        [SecuritySafeCritical]        public static int Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType);        //        // 摘要:        //     比较两个指定的 System.String 对象的子字符串(其中忽略或考虑其大小写,并使用区域性特定的信息干预比较),并返回一个整数,指示二者在排序顺序中的相对位置。        //        // 参数:        //   strA:        //     要在比较中使用的第一个字符串。        //        //   indexA:        //     strA 中子字符串的位置。        //        //   strB:        //     要在比较中使用的第二个字符串。        //        //   indexB:        //     strB 中子字符串的位置。        //        //   length:        //     要比较的子字符串中字符的最大数量。        //        //   ignoreCase:        //     要在比较过程中忽略大小写,则为 true;否则为 false。        //        //   culture:        //     一个对象,提供区域性特定的比较信息。        //        // 返回结果:        //     一个整数,指示两个比较字之间的词法关系。值条件小于零strA 中的子字符串小于 strB 中的子字符串。零子字符串相等,或者 length 为零。大于零strA        //     中的子字符串大于 strB 中的子字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     indexA 大于 strA.System.String.Length。- 或 -indexB 大于 strB.System.String.Length。-        //     或 -indexA、indexB 或 length 为负。- 或 -strA 或 strB 为 null,而 length 大于零。        //        //   System.ArgumentNullException:        //     culture 为 null。        public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture);        //        // 摘要:        //     对两个指定 System.String 对象的子字符串进行比较,使用指定的比较选项和区域性特定的信息来影响比较,并返回一个整数,该整数指示这两个子字符串在排序顺序中的关系。        //        // 参数:        //   strA:        //     要在比较中使用的第一个字符串。        //        //   indexA:        //     strA 中子字符串的起始位置。        //        //   strB:        //     要在比较中使用的第二个字符串。        //        //   indexB:        //     strB 中子字符串的起始位置。        //        //   length:        //     要比较的子字符串中字符的最大数量。        //        //   culture:        //     一个对象,提供区域性特定的比较信息。        //        //   options:        //     要在执行比较时使用的选项(如忽略大小写或符号)。        //        // 返回结果:        //     一个整数,该整数用于指示两个子字符串之间的词法关系,如下表所示。值条件小于零strA 中的子字符串小于 strB 中的子字符串。零两个子字符串相等或者        //     length 为零。大于零strA 中的子字符串大于 strB 中的子字符串。        //        // 异常:        //   System.ArgumentException:        //     options 不是一个 System.Globalization.CompareOptions 值。        //        //   System.ArgumentOutOfRangeException:        //     indexA 大于 strA.Length。- 或 -indexB 大于 strB.Length。- 或 -indexA、indexB 或 length        //     为负。- 或 -strA 或 strB 为 null,而 length 大于零。        //        //   System.ArgumentNullException:        //     culture 为 null。        public static int Compare(string strA, int indexA, string strB, int indexB, int length, CultureInfo culture, CompareOptions options);


2、CompareTo

Compare相似,不同的是它以实例对象本身与制定字符串作比较。

3、Equals

相同返回true,否则false


二、格式化

format

重点关注日期的格式化,。

d 月中的某一天。一位数的日期没有前导零。dd 月中的某一天。一位数的日期有一个前导零。ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。dddd 周中某天的完整名称,在 DayNames 中定义。M 月份数字。一位数的月份没有前导零。MM 月份数字。一位数的月份有一个前导零。MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。MMMM 月份的完整名称,在 MonthNames 中定义。y 不包含纪元的年份。假如不包含纪元的年份小于 10,则显示不具有前导零的年份。yy 不包含纪元的年份。假如不包含纪元的年份小于 10,则显示具有前导零的年份。yyyy 包括纪元的四位数的年份。gg 时期或纪元。假如要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。h 12 小时制的小时。一位数的小时数没有前导零。hh <span style="white-space:pre"></span>12 小时制的小时。一位数的小时数有前导零。H 24 小时制的小时。一位数的小时数没有前导零。HH 24 小时制的小时。一位数的小时数有前导零。m 分钟。一位数的分钟数没有前导零。mm 分钟。一位数的分钟数有一个前导零。s 秒。一位数的秒数没有前导零。ss 秒。一位数的秒数有一个前导零。f 秒的小数精度为一位。其余数字被截断。ff 秒的小数精度为两位。其余数字被截断。fff 秒的小数精度为三位。其余数字被截断。ffff 秒的小数精度为四位。其余数字被截断。fffff 秒的小数精度为五位。其余数字被截断。ffffff 秒的小数精度为六位。其余数字被截断。fffffff 秒的小数精度为七位。其余数字被截断。

定义如下

        //        // 摘要:        //     将指定字符串中的一个或多个格式项替换为指定对象的字符串表示形式。        //        // 参数:        //   format:        //     复合格式字符串。        //        //   arg0:        //     要设置格式的对象。        //        // 返回结果:        //     format 的副本,其中的任何格式项均替换为 arg0 的字符串表示形式。        //        // 异常:        //   System.ArgumentNullException:        //     format 为 null。        //        //   System.FormatException:        //     format 中的格式项无效。- 或 -格式项的索引大于或小于零。        public static string Format(string format, object arg0);        //        // 摘要:        //     将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。        //        // 参数:        //   format:        //     复合格式字符串。        //        //   args:        //     一个对象数组,其中包含零个或多个要设置格式的对象。        //        // 返回结果:        //     format 的副本,其中的格式项已替换为 args 中相应对象的字符串表示形式。        //        // 异常:        //   System.ArgumentNullException:        //     format 或 args 为 null。        //        //   System.FormatException:        //     format 无效。- 或 -格式项的索引小于零或大于等于 args 数组的长度。        public static string Format(string format, params object[] args);        //        // 摘要:        //     将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。指定的参数提供区域性特定的格式设置信息。        //        // 参数:        //   provider:        //     一个提供区域性特定的格式设置信息的对象。        //        //   format:        //     复合格式字符串。        //        //   args:        //     一个对象数组,其中包含零个或多个要设置格式的对象。        //        // 返回结果:        //     format 的副本,其中的格式项已替换为 args 中相应对象的字符串表示形式。        //        // 异常:        //   System.ArgumentNullException:        //     format 或 args 为 null。        //        //   System.FormatException:        //     format 无效。- 或 -格式项的索引小于零或大于等于 args 数组的长度。        [SecuritySafeCritical]        public static string Format(IFormatProvider provider, string format, params object[] args);        //        // 摘要:        //     将指定字符串中的格式项替换为两个指定对象的字符串表示形式。        //        // 参数:        //   format:        //     复合格式字符串。        //        //   arg0:        //     要设置格式的第一个对象。        //        //   arg1:        //     要设置格式的第二个对象。        //        // 返回结果:        //     format 的副本,其中的格式项替换为 arg0 和 arg1 的字符串表示形式。        //        // 异常:        //   System.ArgumentNullException:        //     format 为 null。        //        //   System.FormatException:        //     format 无效。- 或 -格式项的索引小于零或大于一。        public static string Format(string format, object arg0, object arg1);        //        // 摘要:        //     将指定字符串中的格式项替换为三个指定对象的字符串表示形式。        //        // 参数:        //   format:        //     复合格式字符串。        //        //   arg0:        //     要设置格式的第一个对象。        //        //   arg1:        //     要设置格式的第二个对象。        //        //   arg2:        //     要设置格式的第三个对象。        //        // 返回结果:        //     format 的副本,其中的格式项已替换为 arg0、arg1 和 arg2 的字符串表示形式。        //        // 异常:        //   System.ArgumentNullException:        //     format 为 null。        //        //   System.FormatException:        //     format 无效。- 或 -格式项的索引小于零或大于二。        public static string Format(string format, object arg0, object arg1, object arg2);

ps:

int i = 8;

string str = i.ToString("0000");// 将int类型的变量格式化为4位数字


三、截取

public string Substring(int startIndex);public string Substring(int startIndex, int length);
startIndex为起始位置,length为截取长度。如果startIndex为实例长度,且length为0,则返回空。

        //        // 摘要:        //     从此实例检索子字符串。子字符串从指定的字符位置开始。        //        // 参数:        //   startIndex:        //     此实例中子字符串的起始字符位置(从零开始)。        //        // 返回结果:        //     与此实例中在 startIndex 处开头的子字符串等效的一个字符串;如果 startIndex 等于此实例的长度,则为 System.String.Empty。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     startIndex 小于零或大于此实例的长度。        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public string Substring(int startIndex);        //        // 摘要:        //     从此实例检索子字符串。子字符串从指定的字符位置开始且具有指定的长度。        //        // 参数:        //   startIndex:        //     此实例中子字符串的起始字符位置(从零开始)。        //        //   length:        //     子字符串中的字符数。        //        // 返回结果:        //     与此实例中在 startIndex 处开头、长度为 length 的子字符串等效的一个字符串,如果 startIndex 等于此实例的长度且 length        //     为零,则为 System.String.Empty。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     startIndex 加 length 之和指示的位置不在此实例中。- 或 -startIndex 或 length 小于零。        [SecuritySafeCritical]        [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]        public string Substring(int startIndex, int length);

四、分割

public string[] Split(params char[] separator);

将字符串根据separator字符进行分割,返回结果数组。

        //        // 摘要:        //     返回的字符串数组包含此实例中的子字符串(由指定 Unicode 字符数组的元素分隔)。        //        // 参数:        //   separator:        //     分隔此实例中子字符串的 Unicode 字符数组、不包含分隔符的空数组或 null。        //        // 返回结果:        //     一个数组,其元素包含此实例中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。有关更多信息,请参见“备注”部分。        public string[] Split(params char[] separator);        //        // 摘要:        //     返回的字符串数组包含此实例中的子字符串(由指定 Unicode 字符数组的元素分隔)。参数指定返回的子字符串的最大数量。        //        // 参数:        //   separator:        //     分隔此实例中子字符串的 Unicode 字符数组、不包含分隔符的空数组或 null。        //        //   count:        //     要返回的子字符串的最大数量。        //        // 返回结果:        //     一个数组,其元素包含此实例中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。有关更多信息,请参见“备注”部分。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     count 为负数。        public string[] Split(char[] separator, int count);        //        // 摘要:        //     返回的字符串数组包含此字符串中的子字符串(由指定 Unicode 字符数组的元素分隔)。参数指定是否返回空数组元素。        //        // 参数:        //   separator:        //     分隔此字符串中子字符串的 Unicode 字符数组、不包含分隔符的空数组或 null。        //        //   options:        //     要省略返回的数组中的空数组元素,则为 System.StringSplitOptions.RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为        //     System.StringSplitOptions.None。        //        // 返回结果:        //     一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。有关更多信息,请参见“备注”部分。        //        // 异常:        //   System.ArgumentException:        //     options 不是 System.StringSplitOptions 值之一。        [ComVisible(false)]        public string[] Split(char[] separator, StringSplitOptions options);        //        // 摘要:        //     返回的字符串数组包含此字符串中的子字符串(由指定字符串数组的元素分隔)。参数指定是否返回空数组元素。        //        // 参数:        //   separator:        //     分隔此字符串中子字符串的字符串数组、不包含分隔符的空数组或 null。        //        //   options:        //     要省略返回的数组中的空数组元素,则为 System.StringSplitOptions.RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为        //     System.StringSplitOptions.None。        //        // 返回结果:        //     一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符串分隔。有关更多信息,请参见“备注”部分。        //        // 异常:        //   System.ArgumentException:        //     options 不是 System.StringSplitOptions 值之一。        [ComVisible(false)]        public string[] Split(string[] separator, StringSplitOptions options);        //        // 摘要:        //     返回的字符串数组包含此字符串中的子字符串(由指定 Unicode 字符数组的元素分隔)。参数指定要返回子字符串的最大数量,以及是否要返回空数组元素。        //        // 参数:        //   separator:        //     分隔此字符串中子字符串的 Unicode 字符数组、不包含分隔符的空数组或 null。        //        //   count:        //     要返回的子字符串的最大数量。        //        //   options:        //     要省略返回的数组中的空数组元素,则为 System.StringSplitOptions.RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为        //     System.StringSplitOptions.None。        //        // 返回结果:        //     一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符分隔。有关更多信息,请参见“备注”部分。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     count 为负数。        //        //   System.ArgumentException:        //     options 不是 System.StringSplitOptions 值之一。        [ComVisible(false)]        public string[] Split(char[] separator, int count, StringSplitOptions options);        //        // 摘要:        //     返回的字符串数组包含此字符串中的子字符串(由指定字符串数组的元素分隔)。参数指定要返回子字符串的最大数量,以及是否要返回空数组元素。        //        // 参数:        //   separator:        //     分隔此字符串中子字符串的字符串数组、不包含分隔符的空数组或 null。        //        //   count:        //     要返回的子字符串的最大数量。        //        //   options:        //     要省略返回的数组中的空数组元素,则为 System.StringSplitOptions.RemoveEmptyEntries;要包含返回的数组中的空数组元素,则为        //     System.StringSplitOptions.None。        //        // 返回结果:        //     一个数组,其元素包含此字符串中的子字符串,这些子字符串由 separator 中的一个或多个字符串分隔。有关更多信息,请参见“备注”部分。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     count 为负数。        //        //   System.ArgumentException:        //     options 不是 System.StringSplitOptions 值之一。        [ComVisible(false)]        public string[] Split(string[] separator, int count, StringSplitOptions options);

五、插入

public string Insert(int startIndex, string value);

在startIndex位置插入value字符串。

        //        // 摘要:        //     在此实例中的指定索引位置插入一个指定的 System.String 实例。        //        // 参数:        //   startIndex:        //     此插入的索引位置。        //        //   value:        //     要插入的字符串。        //        // 返回结果:        //     与此实例等效的一个新字符串,但在该字符串的 startIndex 位置处插入了 value。        //        // 异常:        //   System.ArgumentNullException:        //     value 为 null。        //        //   System.ArgumentOutOfRangeException:        //     startIndex 为负,或大于此实例的长度。        [SecuritySafeCritical]        public string Insert(int startIndex, string value);

六、填充

public string PadLeft(int totalWidth, char paddingChar);public string PadRight(int totalWidth, char paddingChar);
totalWidth:结果字符串的字符数,等于原始字符数加上填充字符长度。

paddingChar:填充字符

返回值:等效于此实例的一个新String,在左或右以达到原始字符串长度所需数目paddingChar进行填充。如果totalWidth小于实例长度,返回此实例相同的新String.

例子:

string strTest = "haha";string strTestRst = strTest.PadLeft(strTest.Length + 2, 'c');// 'cchaha'
定义:

        //        // 摘要:        //     返回一个新字符串,该字符串通过在此实例中的字符左侧填充空格来达到指定的总长度,从而实现右对齐。        //        // 参数:        //   totalWidth:        //     结果字符串中的字符数,等于原始字符数加上任何其他填充字符。        //        // 返回结果:        //     与此实例等效的一个新字符串,但该字符串为右对齐,因此,在左侧填充所需数量的空格,使长度达到 totalWidth。如果 totalWidth 小于此实例的长度,则为与此实例相同的新字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     totalWidth 小于零。        public string PadLeft(int totalWidth);        //        // 摘要:        //     返回一个新字符串,该字符串通过在此实例中的字符左侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符右对齐。        //        // 参数:        //   totalWidth:        //     结果字符串中的字符数,等于原始字符数加上任何其他填充字符。        //        //   paddingChar:        //     Unicode 填充字符。        //        // 返回结果:        //     与此实例等效的一个新字符串,但该字符串为右对齐,因此,在左侧填充所需任意数量的 paddingChar 字符,使长度达到 totalWidth。如果        //     totalWidth 小于此实例的长度,则为与此实例相同的新字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     totalWidth 小于零。        public string PadLeft(int totalWidth, char paddingChar);        //        // 摘要:        //     返回一个新字符串,该字符串通过在此字符串中的字符右侧填充空格来达到指定的总长度,从而使这些字符左对齐。        //        // 参数:        //   totalWidth:        //     结果字符串中的字符数,等于原始字符数加上任何其他填充字符。        //        // 返回结果:        //     与此实例等效的一个新字符串,但该字符串为左对齐,因此,在右侧填充所需任意数量的空格,使长度达到 totalWidth。如果 totalWidth        //     小于此实例的长度,则为与此实例相同的新字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     totalWidth 小于零。        public string PadRight(int totalWidth);        //        // 摘要:        //     返回一个新字符串,该字符串通过在此字符串中的字符右侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符左对齐。        //        // 参数:        //   totalWidth:        //     结果字符串中的字符数,等于原始字符数加上任何其他填充字符。        //        //   paddingChar:        //     Unicode 填充字符。        //        // 返回结果:        //     与此实例等效的一个新字符串,但该字符串为左对齐,因此,在右侧填充所需任意数量的 paddingChar 字符,使长度达到 totalWidth。如果        //     totalWidth 小于此实例的长度,则为与此实例相同的新字符串。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     totalWidth 小于零。        public string PadRight(int totalWidth, char paddingChar);

七、删除

public string Remove(int startIndex);public string Remove(int startIndex, int count);

从startIndex开始,删除count数目字符,返回一个新的string字符串。第一种语法格式删除指定位置到最后位置的所有字符。

定义:

        //        // 摘要:        //     删除此字符串中从指定位置到最后位置的所有字符。        //        // 参数:        //   startIndex:        //     开始删除字符的从零开始的位置。        //        // 返回结果:        //     一个新字符串,除所删除的字符之外,该字符串与此字符串等效。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     startIndex 小于零。- 或 -startIndex 指定的位置不在此字符串内。        public string Remove(int startIndex);        //        // 摘要:        //     从此实例中的指定位置开始删除指定数目的字符。        //        // 参数:        //   startIndex:        //     开始删除字符的从零开始的位置。        //        //   count:        //     要删除的字符数。        //        // 返回结果:        //     一个新字符串,除所删除的字符之外,该字符串与此实例等效。        //        // 异常:        //   System.ArgumentOutOfRangeException:        //     startIndex 或 count 小于零。- 或 -startIndex 加 count 之和指定一个此实例外的位置。        [SecuritySafeCritical]        public string Remove(int startIndex, int count);

八、复制

public static string Copy(string str);public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

将字符串或子字符串复制到另一个字符串或者char结构型的数组中。

例:

string strTest = "haha";string strTestRst = string.Copy(strTest);// haha

定义:

        //        // 摘要:        //     创建一个与指定的 System.String 具有相同值的 System.String 的新实例。        //        // 参数:        //   str:        //     要复制的字符串。        //        // 返回结果:        //     值与 str 相同的新字符串。        //        // 异常:        //   System.ArgumentNullException:        //     str 为 null。        [SecuritySafeCritical]        public static string Copy(string str);        //        // 摘要:        //     将指定数目的字符从此实例中的指定位置复制到 Unicode 字符数组中的指定位置。        //        // 参数:        //   sourceIndex:        //     要复制的此实例中第一个字符的索引。        //        //   destination:        //     此实例中的字符所复制到的 Unicode 字符数组。        //        //   destinationIndex:        //     destination 中的索引,在此处开始复制操作。        //        //   count:        //     此实例中要复制到 destination 的字符数。        //        // 异常:        //   System.ArgumentNullException:        //     destination 为 null。        //        //   System.ArgumentOutOfRangeException:        //     sourceIndex、destinationIndex 或 count 为负- 或 -count 大于从 startIndex 到此实例末尾的子字符串的长度-        //     或 -count 大于从 destinationIndex 到 destination 末尾的子数组的长度        [SecuritySafeCritical]        public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

九、替换

public string Replace(char oldChar, char newChar);public string Replace(string oldValue, string newValue);

替换目标字符串中的单个字符,或者子字符串。


0 0
原创粉丝点击