C#字符串的操作及方法归纳(二)

C#字符串的操作及方法归纳(二)

  • 字符串的分割

    public String[] Split(params char[] separator); //以字符数组分割字符串,返回字符串数组
    
    string str = "Hello World!";
    char[] separator = {' ', '!'};
    string[] changeStr = str.Split(separator);
    foreach (string s in changeStr)
    {
        Console.WriteLine(s);//输出:"Hello"、"World"、""
    }
    

    注意:被分割的字符串包含空字符串("")。

  • 字符串的合并

    1、“+”

    连接运算符“+”可以方便地连接多个字符串、数字类型等。
    
    string str1 = "Hello";
    string str2 = "World";
    Console.WriteLine(str1 + str2 + 123);//输出:HelloWorld123
    

    2、Concat

    public static String Concat(params String[] values); //将字符串相加,返回相加后地字符串
    
    public static String Concat(params object[] args); //将object类型相加,返回相加后地字符串
    
    string str1 = "Hello";
    string str2 = "World";
    char c = '!';
    Console.WriteLine(string.Concat(str1, str2, c));//输出:HelloWorld!
    

    3、Join

    //在字符串数组中间加入separator字符串,返回新的字符串
    public static String Join(String separator, params String[] value);
    
    //在object数组中间加入separator字符串,返回新的字符串
    public static String Join(String separator, params object[] values);
    
    string[] names = { "张三", "李四", "王五", "刘六" };
    Console.WriteLine(string.Join("-", names));//输出:张三-李四-王五-刘六
    Console.WriteLine(string.Join("*", 1, 5.0, 'c', "hi"));//输出:1*5*c*hi
    
  • 字符串的插入

    public String Insert(int startIndex, String value); //在下标为startIndex位置插入字符串value
    
    string str = "HelloWorld";
    Console.WriteLine(str.Insert(1, " "));//输出:H elloWorld
    Console.WriteLine(str.Insert(10, "?"));//输出:HelloWorld?
    

    注意:下标索引不能超过字符串长度,否则会出现异常。

  • 字符串的填充

    //将长度小于totalWidth的字符串,从左边填充默认的空格符号
    public String PadLeft(int totalWidth);
    
    //从左边填充默认的paddingChar字符
    public String PadLeft(int totalWidth, char paddingChar);
    
    //从右边填充空格符号
    public String PadRight(int totalWidth);
    
    //从右边填充paddingChar符号
    public String PadRight(int totalWidth, char paddingChar);
    
    string str1 = "A";
    string str2 = "BB";
    string str3 = "CCCCCCC";
    Console.WriteLine(str1.PadLeft(5));//输出:    A
    Console.WriteLine(str2.PadLeft(5, '*'));//输出:***BB
    Console.WriteLine(str3.PadLeft(5, '*'));//输出:CCCCCCC
    Console.WriteLine(str1.PadRight(5));//输出:A    (光标处)
    Console.WriteLine(str2.PadRight(5, '*'));//输出:BB***
    Console.WriteLine(str3.PadRight(5, '*'));//输出:CCCCCCC
    

    注意:字符串长度大于要求长度时,输出原来的字符串。

  • 字符串的删除

    //将下标为startIndex(包括startIndex)到字符串后面删除
    public String Remove(int startIndex);
    
    //将下标为startIndex(包括startIndex)到后面count个字符删除
    public String Remove(int startIndex, int count);
    
    string str = "abcdefg123456";
    Console.WriteLine(str.Remove(4));//输出:abcd
    Console.WriteLine(str.Remove(4, 5));//输出:abcd3456
    

因为作者精力有限,文章中难免出现一些错漏,敬请广大专家和网友批评、指正。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值