C#中字符串的处理

本文介绍了C#中字符串处理的基本操作,包括大小写转换、去除空白字符及特定字符的方法。通过实例展示了ToLower、ToUpper、Trim及Split等方法的具体应用。
在C#中,有时候我们会对一个声明的字符串进行一些处理,比如说将字符串中的英文字符进行大小写转换,将字符串前后的或者中间的空字符去掉,或者是 将字符串中的一些同类型的字符去掉等等,接下来,让我们一起学习吧!
  • ToLower:将字符串中的英文字母全部准换成小写
  • ToUpper:将字符串中的英文字母全部转换成大写
  • Trim :将字符串前后的空白字符删除;其中 TrimStart是将字符串开头的空白字符删除;TrimEnd是将字符串后面的空白字符删除
  • Split :是将规定的字符删除,其中Split返回的值同样也是一个数组
例子:
①.对于ToLower的使用:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "I'm a student! I'm a good man!";
            string res = str.ToLower();
            Console.WriteLine( res);
        }
    }
}
 
②.对于ToUpper的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "I'm a student! I'm a good man!";
            string res = str.ToUpper();
            Console.WriteLine( res);
        }
    }
}
 
③.对于Trim的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "   I'm a student! I'm a good man!   ";
            string res = str.Trim();
            Console.WriteLine( str + "|");
            Console.WriteLine( res);
        }
    }
}
 
其中对于TrimStart和TrimEnd这两个。在此不在举例了,有兴趣的可以自己敲一下。
④:对于Spilt的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "   I'm a student! I'm a good man!   ";
            string[] res = str.Split('!');// split返回的值为一个字符串数组,在这里直接将str数组中的!删除,只打印其他内容
            Console.WriteLine( str + "|");
            foreach (string i in res)// 遍历数组
            {
                Console.WriteLine(i);
            }
           
        }
    }
}
 

转载于:https://www.cnblogs.com/ze871895381/p/5682351.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值