C#中String类的属性及方法有哪些

发布时间:2022-03-31 17:26:35 作者:iii
来源:亿速云 阅读:240

今天小编给大家分享一下C#中String类的属性及方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

String 类的属性

String 类有以下两个属性:

序号属性名称 & 描述
1Chars
在当前 String 对象中获取 Char 对象的指定位置。
2Length
在当前的 String 对象中获取字符数。

String 类的方法

String 类有许多方法用于 string 对象的操作。下面的表格提供了一些最常用的方法:

序号方法名称 & 描述
1public static int Compare( string strA, string strB )
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。该方法区分大小写。
2public static int Compare( string strA, string strB, bool ignoreCase )
比较两个指定的 string 对象,并返回一个表示它们在排列顺序中相对位置的整数。但是,如果布尔参数为真时,该方法不区分大小写。
3public static string Concat( string str0, string str1 )
连接两个 string 对象。
4public static string Concat( string str0, string str1, string str2 )
连接三个 string 对象。
5public static string Concat( string str0, string str1, string str2, string str3 )
连接四个 string 对象。
6public bool Contains( string value )
返回一个表示指定 string 对象是否出现在字符串中的值。
7public static string Copy( string str )
创建一个与指定字符串具有相同值的新的 String 对象。
8public void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count )
从 string 对象的指定位置开始复制指定数量的字符到 Unicode 字符数组中的指定位置。
9public bool EndsWith( string value )
判断 string 对象的结尾是否匹配指定的字符串。
10public bool Equals( string value )
判断当前的 string 对象是否与指定的 string 对象具有相同的值。
11public static bool Equals( string a, string b )
判断两个指定的 string 对象是否具有相同的值。
12public static string Format( string format, Object arg0 )
把指定字符串中一个或多个格式项替换为指定对象的字符串表示形式。
13public int IndexOf( char value )
返回指定 Unicode 字符在当前字符串中第一次出现的索引,索引从 0 开始。
14public int IndexOf( string value )
返回指定字符串在该实例中第一次出现的索引,索引从 0 开始。
15public int IndexOf( char value, int startIndex )
返回指定 Unicode 字符从该字符串中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
16public int IndexOf( string value, int startIndex )
返回指定字符串从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
17public int IndexOfAny( char[] anyOf )
返回某一个指定的 Unicode 字符数组中任意字符在该实例中第一次出现的索引,索引从 0 开始。
18public int IndexOfAny( char[] anyOf, int startIndex )
返回某一个指定的 Unicode 字符数组中任意字符从该实例中指定字符位置开始搜索第一次出现的索引,索引从 0 开始。
19public string Insert( int startIndex, string value )
返回一个新的字符串,其中,指定的字符串被插入在当前 string 对象的指定索引位置。
20public static bool IsNullOrEmpty( string value )
指示指定的字符串是否为 null 或者是否为一个空的字符串。
21public static string Join( string separator, string[] value )
连接一个字符串数组中的所有元素,使用指定的分隔符分隔每个元素。
22public static string Join( string separator, string[] value, int startIndex, int count )
连接一个字符串数组中的指定位置开始的指定元素,使用指定的分隔符分隔每个元素。
23public int LastIndexOf( char value )
返回指定 Unicode 字符在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
24public int LastIndexOf( string value )
返回指定字符串在当前 string 对象中最后一次出现的索引位置,索引从 0 开始。
25public string Remove( int startIndex )
移除当前实例中的所有字符,从指定位置开始,一直到最后一个位置为止,并返回字符串。
26public string Remove( int startIndex, int count )
从当前字符串的指定位置开始移除指定数量的字符,并返回字符串。
27public string Replace( char oldChar, char newChar )
把当前 string 对象中,所有指定的 Unicode 字符替换为另一个指定的 Unicode 字符,并返回新的字符串。
28public string Replace( string oldValue, string newValue )
把当前 string 对象中,所有指定的字符串替换为另一个指定的字符串,并返回新的字符串。
29public string[] Split( params char[] separator )
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。
30public string[] Split( char[] separator, int count )
返回一个字符串数组,包含当前的 string 对象中的子字符串,子字符串是使用指定的 Unicode 字符数组中的元素进行分隔的。int 参数指定要返回的子字符串的最大数目。
31public bool StartsWith( string value )
判断字符串实例的开头是否匹配指定的字符串。
32public char[] ToCharArray()
返回一个带有当前 string 对象中所有字符的 Unicode 字符数组。
33public char[] ToCharArray( int startIndex, int length )
返回一个带有当前 string 对象中所有字符的 Unicode 字符数组,从指定的索引开始,直到指定的长度为止。
34public string ToLower()
把字符串转换为小写并返回。
35public string ToUpper()
把字符串转换为大写并返回。
36public string Trim()
移除当前 String 对象中的所有前导空白字符和后置空白字符。

上面的方法列表并不详尽,请访问 MSDN 库,查看完整的方法列表和 String 类构造函数。

以上就是“C#中String类的属性及方法有哪些”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

推荐阅读:
  1. String类型的属性和方法
  2. C#中的类:字段、属性、方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

string

上一篇:C#中怎么创建String对象

下一篇:C#中怎么比较字符串

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》