C#中字符串的处理

标签: c#stringclassc
1162人阅读 评论(1) 收藏 举报

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

namespace 字符串处理
{
    class Program
    {
        static void Main(string[] args)
        {
            //string Str1 = "友情相逢";
            //string Str2 = "用一生爱你";
            //#region char的使用
            //char a = 'a';
            //Console.WriteLine("IsLetter方法判断a是否为字母:{0}", Char.IsLetter(a));
            //Console.WriteLine("IsDigit方法判断a是否为数字:{0}", Char.IsDigit(a));
            //Console.WriteLine("IsLower方法判断a是否为小写字母:{0}", Char.IsLower(a));
            //Console.WriteLine("IsUpper方法判断a是否为大写字母:{0}", Char.IsUpper(a));
            //Console.WriteLine("IsWhiteSpace方法判断a是否为空白:{0}", Char.IsWhiteSpace(a));
            //#endregion

            //#region String类的使用
            //string Str1 = "用一生爱护你!";
            //char Str2 = Str1[1];
            //char Str3 = Str1[2];
            //Console.WriteLine("字符串Str1中第二个字符是:{0}", Str2);
            //Console.WriteLine("字符串Str1中第三个字符是:{0}", Str3);
            //#endregion

            //#region Conpare/CompareTo比较字符串的使用
            //Console.WriteLine(string.Compare(Str1, Str2));
            //Console.WriteLine(string.Compare(Str1, Str1));
            //Console.WriteLine(string.Compare(Str2, Str1));

            //Console.WriteLine(Str1.CompareTo(Str2));

            //Console.WriteLine(string.Equals(Str1,Str2)); //判断两个字符串是否相等
            //#endregion

            //#region ForMat格式化输出字符串
            //string newstr = String.Format("{0},{1}!!!", Str1, Str2); //ForMat格式化字符串
            //Console.WriteLine(newstr);
            //DateTime dt = DateTime.Now;
            //string time1 = string.Format("{0:D}", dt);
            //string time2 = string.Format("{0:T}", dt);
            //string time3 = string.Format("{0:G}", dt);
            //Console.Write(time1);
            //Console.WriteLine(time2);
            //Console.WriteLine(time3);
            //#endregion


            //#region 截取字符串
            //string Str3 = "";
            //Str3 = Str1.Substring(1, 3);
            //Console.WriteLine(Str3);
            //#endregion

            //#region 分割字符串
            //string Str3 = "用!一生,下载";
            //char[] separator = { '!', ',' };
            //String[] splitstrings = new String[20];
            //splitstrings = Str3.Split(separator);
            //for (int i = 0; i <= splitstrings.Length; i++)
            //{
            //    Console.WriteLine("item{0}:{1}", i, splitstrings[i]);
            //}
            //#endregion

            //#region 插入和填充字符串
            //string str3 = "下载";
            //string str4;
            //str4 = str3.Insert(0, "用一生");
            //string str5 = str4.Insert(5, "你");
            //Console.WriteLine(str5);

            //string str6 = "*+_+*";
            //string str7 = str6.PadLeft(6, '(');     //在字符串左侧添加,确保添加后的总字符串为6
            //string str8 = str7.PadRight(7, ')');
            //Console.WriteLine("补充字符串之前" + str6);
            //Console.WriteLine("补充字符串之后" + str8);
            //#endregion

            //#region 删除字符串
            //string str3 = "用一生下载你";
            //string str4 = str3.Remove(3);  //3为所以字符 往后的都删掉
            //Console.WriteLine(str4);

            //string str5 = str3.Remove(0, 3);  //0为索引,3为删除的字符数
            //Console.WriteLine(str5);
            //#endregion

            //#region 字符串的复制
            //string str = "我爱你forever";
            //string Str;
            //Str = String.Copy(str);   //将str复制给Str
            //Console.WriteLine(Str);

            //string str3 = "用一生下载你";
            //char[] str4 = new char[100];
            //str3.CopyTo(1, str4, 0, 4);     //第一个参数是原字符要复制的起始位置,第二个是新字符,第三个是新字符的复制的起始位置,第四个是复制的字符个数
            //Console.WriteLine(str4);
            //#endregion

            //string a = "one world ,one dream";
            //string b = a.Replace(',','*');    // 字符的替换用单引号
            //Console.WriteLine(b);
            //string c = a.Replace("one world","One World");  //字符串替换用双引号
            //Console.WriteLine(c);
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1529233次
    • 积分:20130
    • 等级:
    • 排名:第399名
    • 原创:407篇
    • 转载:215篇
    • 译文:3篇
    • 评论:399条
    关于我
    我叫小未,网名Aladdin,主做U3D游戏程序,某普通本科学校毕业,但不甘于平庸,喜欢尝试,喜欢研究计算机技术,13年9月份开始工作,曾两年就职于巨人网络做游戏客户端程序,闲暇时间做过麦子学院在线讲师,做过独立开发,做过C#游戏服务器和.NET Web服务器,做过IOS原生开发,爱游戏,爱coding!

    邮件:1213250243@qq.com
    Git仓库
    友情链接
    新浪围脖
    QQ交流
    博客专栏
    最新评论