首页 > 编程知识 正文

c语言string类型(c语言字符串数组)

时间:2023-05-03 18:12:33 阅读:83144 作者:4543

在C#中,可以使用字符数组来表示字符串,但通常使用字符串关键字来声明字符串变量。 string关键字是System.String类的别名。

创建字符串对象

可以使用以下方法之一创建string对象:

为字符串变量指定字符串

通过使用String类的构造函数

使用字符串连接运算符(

调用获取属性或返回字符串的方法

用格式方法将值或对象转换为字符串表示形式

以下示例说明了这一点。

单一系统; namespace StringApplication{上面的代码被编译并执行后,将产生以下结果。

全名3360 Rowanatkinsongreetings 3360 Hello消息3360 Hellofromtutorialspointmessage 3360消息传感站17336058 on Wednesday

字符串类的属性

String类有两个属性:

序列号属性名称说明1Chars获取当前String对象中Char对象的指定位置。 2Length获取当前String对象的字符数。 String类的方法

String类提供了许多用于处理String对象的方法。 下表列出了最常见的方法。

序列号方法名称将比较两个指定了1 publicstaticintcompare (string stra,string strB )的string对象,并返回表示排序相对位置的整数。 这个方法区分大小写。 2公共字符串比较器(string strB、string strB、bool ignoreCase ) )比较两个指定的字符串对象,并返回表示排序相对位置的整数。 但是,如果布尔参数为真,则此方法不区分大小写。 3 publicstaticstringconcat (stringstr 0,string str1)连接两个字符串对象。 4 publicstaticstringconcat (stringstr 0,string str1,string str2)连接三个字符串对象。 5公共字符串连接器(stringstr 0,string str1,string str2,string str3)连接四个字符串对象。 6公共布尔容器(字符串值)返回一个值,指示字符串中是否存在指定的字符串对象。 7 publicstaticstringcopy (stringstr )创建一个具有与指定字符串相同值的新string对象。 8公共语音复制到(intsourceindex,char [ ]目的地,int destinationIndex,int count ) )是从字符串对象的指定位置开始指定的9公共布尔值(string value )确定string对象的结尾是否与指定的字符串匹配。 10公共布尔值(string value )确定当前string对象是否与指定的string对象相同。 11 publicstaticboolequals (stringa,string b )确定两个指定的string对象是否具有相同的值。 12公共静态字符串格式(string format,Object arg0)将指定字符串中的一个或多个格式项目替换为指定对象的字符串表示形式。 13公共索引(字符值)返回指定的Unicode字符在当前字符串中首次出现的索引。 索引从0开始。 14公共索引(字符串值)返回指定字符串在此实例中首次出现的索引。 索引从0开始。 15公共索引(char value,int startIndex )搜索指定的Unicode字符从字符串中的指定字符位置首先出现的索引,并返回索引从0开始。 16publicintindexof(stringvalue,int startIndex ) )查找指定字符串从实例中的指定字符位置首次出现的索引,并返回索引从0开始。 17公共输入

dexOfAny( 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 类构造函数。

实例

下面的实例演示了上面提到的一些方法:

比较字符串

using System;namespace StringApplication{

当上面的代码被编译和执行时,它会产生下列结果:

This is test and This is text are not equal.

字符串包含字符串:

using System;namespace StringApplication{

当上面的代码被编译和执行时,它会产生下列结果:

The sequence 'test' was found.

获取子字符串:

using System; namespace StringApplication {

运行实例 »

当上面的代码被编译和执行时,它会产生下列结果:

Last night I dreamt of San PedroSan Pedro

连接字符串:

using System;namespace StringApplication{

当上面的代码被编译和执行时,它会产生下列结果:

Down the way nights are darkAnd the sun shines daily on the mountain top

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。