文章目录 索引字符串( Index ) 遍历字符串( for ) 拆分字符串( Split ) 替代字符串( Replace ) 提取字符串( Substring ) 插入字符串( Insert ) 移除字符串( Remove )
索引字符串( Index )
当我们要查看字符串中的某个字符时,我们可以使用索引功能。
string ax = "我是大帅哥" ; char temp = ax[ 2 ] ;
Console. WriteLine ( temp) ;
查看运行效果:
>> > 大
遍历字符串( for )
遍历字符串可以使用for语句,通过循环迭代的索引值,获取字符串的每一个字。 其中ax.Length 表示获取字符串中的字符个数。
using System ;
namespace tiamo
{ public class Program { static void Main ( string [ ] args) { string ax = "我是大帅哥" ; for ( int i = 0 ; i < ax. Length; i++ ) { Console. WriteLine ( ax[ i] ) ; } } }
}
查看运行效果:
>> > 我
>> > 是
>> > 大
>> > 帅
>> > 哥
拆分字符串( Split )
拆分字符串我们通常采用Split()方法,它会以某个字符作为拆分点,并将拆分下来的字符串或字符转变为一个字符串数组。 Split 方法是 System.String 类的一个静态方法,用于将字符串拆分为字符串数组。 该方法的具体语法:Spilt(“分割点”) 所以我们需要通过遍历才能拿查看数组内的所有值。 虽然foreach()无法直接遍历字符串类型,但可以遍历出数组类型中的字符串。 其中Split(“,”)中的,表示将,作为拆分点。更多相关可参考 官方文档
string ax = "我是大帅哥,我很想低调,但这很难" ; string [ ] temp = ax. Split ( "," ) ; foreach ( string x in temp)
{ Console. WriteLine ( x) ;
}
查看运行效果:
>> > 我是大帅哥
>> > 我很想低调
>> > 但这很难
替代字符串( Replace )
完美可以使用Replace( )方法来实现替代字符串。 该方法的具体语法:Replace(“被替代的”, “要替代的” )
string ax = "精益求精,至臻至善" ; string temp = ax. Replace ( "至臻至善" , "成就完美" ) ; Console. WriteLine ( temp) ;
查看运行效果:
>> > 精益求精,成就完美
提取字符串( Substring )
使用Substring( )方法可以从指定的索引值开始提取。 该方法的具体语法:Substring(index, length) ,其中index表示该索引值开始提取,length表示提取的字符长度,如果只指定一个参数,则默认为index。
string ax = "我是大帅哥" ; string temp = ax. Substring ( 2 ) ;
Console. WriteLine ( temp) ; string temp_2 = ax. Substring ( 1 , 3 ) ;
Console. WriteLine ( temp_2) ;
查看运行效果:
>> > 大帅哥
>> > 是大帅
插入字符串( Insert )
Insert( )方法为我们提供了插入字符串的功能。 Insert 方法是 StringBuilder 类的一个成员,它用于在 StringBuilder 对象的指定位置插入一个字符串。 该方法的具体语法:**Insert(index, value)**其中,index表示在该索引值后面插入,value表示要插入的字符串或字符。
string ax = "我是大帅哥" ; string temp = ax. Insert ( 2 , "超级" ) ;
Console. WriteLine ( temp) ;
>> > 我是超级大帅哥
移除字符串( Remove )
Remove 方法是 StringBuilder 类的一个成员,它用于从 StringBuilder 对象中删除指定范围的字符。 该方法的具体语法:Remove(index, value) ,其中index表示该索引值开始移除,length表示移除的字符长度,如果只指定一个参数,则默认为index,如果为0,则表示全部移除为空
string ax = "我是大帅哥" ; string temp = ax. Remove ( 1 ) ;
Console. WriteLine ( temp) ; string temp_2 = ax. Remove ( 2 , 2 ) ;
Console. WriteLine ( temp_2) ;
>> > 我是
>> > 我是哥