C#编程--字符串处理

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

namespace ConsoleApplication1
{
    class Program
    {
       
        static void Main(string[] args)
        {
            //字符串处理
            string s = "hello";

            //1,得到字符串的长度
            Console.WriteLine("字符串的长度"+s.Length);

            //2,利用数组操作字符串 只能读不能写 字符串一旦声明 ,不能改变
            Console.WriteLine("第一个字符" + s[0]);

            //3,转换为字符
            char[] arr = s.ToCharArray();
            arr[0] = 'A';

            //4,利用数组构造字符串
            s = new string(arr);
            Console.WriteLine(s);

            //5,字符串转换成小写
            String t = s.ToLower();
            Console.WriteLine(t);

            //6,转换成大写
            t = s.ToUpper();
            Console.WriteLine(t);

            //7,去掉空白两边
            Console.WriteLine(s.Trim());

            //8,字符串的比较
            Console.WriteLine("aello".Equals(s));
            Console.WriteLine("aello".Equals(s, StringComparison.OrdinalIgnoreCase)); //忽略大小写的比较
            Console.WriteLine("Aello" == s); //==区分大小写的比较

            //9,分割字符串
            s = "aaa,bbb,ccc,,   ddd|sdf|dddd|ddd";
            string[] strArr = s.Split(',','|');
            foreach (string temp in strArr)
            {
                Console.WriteLine(temp);
            }
            //             也可以换成字符串的数组
            strArr = s.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries) ;//移除空的元素
            foreach(string strtemp in strArr)
            {
                Console.WriteLine(strtemp);
            }

            //10.字符串替换
            s = "11111";
            string temp2 = s.Replace("11", "22");
            Console.WriteLine(temp2);

            //11,字符串截取
            s = "1234567890";
            Console.WriteLine(s.Substring(2));  //从索引2号开始截取
            Console.WriteLine(s.Substring(2,4));  //                           ,截取4个字符

            //12,判断是否含有子川
            Console.WriteLine(s.Contains("345"));

            //13,判断字符串是否以什么开头
            Console.WriteLine(s.StartsWith("123"));
            //s.EndsWith();



            Console.ReadKey();
        }
    }
}
 

本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1076244

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值