C#常用的字符串函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _2152
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "aBc";
            char[] chrs = str1.ToCharArray();//把string转化为char[]
            foreach(char chr in chrs)
            {
                Console.WriteLine(chr);
            }

            str1 = str1.ToLower();//返回string类型,把字符串变为小写
            Console.WriteLine(str1);

            str1 = str1.ToUpper();//返回string类型,把字符串变为大写
            Console.WriteLine(str1);

            bool b = str1.Equals("ABC", StringComparison.OrdinalIgnoreCase);//忽略大小写比较
            Console.WriteLine(b);

            string str2 = "    what a good day!     ";
            str2 = str2.Trim();//去掉字符串两边的红白,中间的空白不去除
            Console.WriteLine(str2);

            string str3 = "aa,b|c,ff.h";
            string[] str4 = str3.Split(',', '|', '.');
            foreach(string item1 in str4)
            {
                Console.WriteLine(item1);
            }

            string str5 = ",,c,,b,c,,,,c";
            string[] str6 = str5.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string item2 in str6)
            {
                Console.WriteLine(item2);
            }

            string str7 = "我是狗我是猫我是王八蛋";
            string[] str8 = str7.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries);
            foreach(string item3 in str8)
            {
                Console.WriteLine(item3);
            }

            str7 = str7.Replace("我是", "你是");//原型string Replace(string oldString, string newString);//用新串替换旧窜
            Console.WriteLine(str7);

            str7 = str7.Substring(6);//得到从某个位置开始至字符串末尾的子串
            Console.WriteLine(str7);

            str7 = str7.Substring(0, 2);//得到从某个位置开始并且长度为length的子串
            Console.WriteLine(str7);

            string str9 = "what a good day! Let's party,goodbye";
            bool b1 = str9.Contains("what");//判断串中是否含有某个串
            Console.WriteLine(b1);

            bool b2 = str9.StartsWith("where");//判断串是否是以某个串开始
            Console.WriteLine(b2);

            bool b3 = str9.EndsWith("bye");//判断串是否是以某个串结束
            Console.WriteLine(b3);

            int ipos = str9.IndexOf("day");//某个串第一次出现的位置
            Console.WriteLine(ipos);

            Console.ReadKey();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值