【C#】常用字符串方法

常用方法列表

  • ToCharArray()将字符串转为char类型数组
  • new string(char[] chs)将字符数组转为字符串
  • Length获取字符串长度
  • ToUpper()字符串转为首字母大写
  • ToLower()将字符串首字母转为小写
  • 判断两个字符串是否相等,Equals, 默认区分大小写,第二个参数StringComparison.OrdinalIgnoreCase可以忽略大小写
  • Split字符串分割 ,StringSplitOptions.RemoveEmptyEntries 移除空元素

代码示例

using System;

using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "hello";
            // ToCharArray()将字符串转为char类型数组
            char[] chs = s.ToCharArray();
            chs[0] = 'b';
            Console.WriteLine(chs);
            // new string(char[] chs)将字符数组转为字符串
            string s2 = new string(chs);
            Console.WriteLine(s2);
            // Length获取字符串长度
            Console.WriteLine(s2.Length);
            // ToUpper()字符串转为首字母大写
            Console.WriteLine(s2.ToUpper());
            // ToLower()将字符串首字母转为小写
            Console.WriteLine(s2.ToLower());
            // 判断两个字符串是否相等,Equals, 默认区分大小写,第二个参数StringComparison.OrdinalIgnoreCase可以忽略大小写
            Console.WriteLine(s2.Equals(s2.ToUpper()));
            Console.WriteLine(s2.Equals(s2.ToUpper(), StringComparison.OrdinalIgnoreCase));

            string s3 = "a b fds _ +,= ";
            char[] chars = {' ','_','+','=', ','};
            // Split字符串分割 ,StringSplitOptions.RemoveEmptyEntries 移除空元素
            string[] strArr = s3.Split(chars, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine(strArr.Length);
            Console.ReadKey();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值