C#学习Day05

c#学习day05

1、练习字符串的常见方法

Format(格式化):有几个占位符,就需要几个参数,占位符顺序也必须一致

int Grade = 3;

int Class= 7;

Console.WriteLine(string.Format("{0}级{1}班",Grade,Class));

//显示3级7班

IsNullOrEmpty和IsNullOrWhiteSpace:判断是否为NULL、空、或空格

string TestNull=null;

string TestEmpty = "";

string TestWhiteSpace = " ";

if(string.IsNullOrEmpty(TestNull))

{

    Console.WriteLine("TestNull is Null!");

}

if(string.IsNullOrEmpty(TestEmpty))

{

    Console.WriteLine("TestEmpty is Empty!");

}

if(string.IsNullOrWhiteSpace(TestWhiteSpace))

{

    Console.WriteLine("TestWhiteSpace is WhiteSpace!");

}

Equals(匹配):对字母大小敏感,适用于对象之间的匹配

string TestStr = "Hello World";

if (TestStr.ToLower().Equals("hello world"))

//ToLower字符串全小写,ToUpper字符串全大写

{

    Console.WriteLine("匹配成功!");

}

else

{

    Console.WriteLine("匹配失败!");

}

Contains:判断是否包含某个字符或字符串

string TestStr = "Hello World";

if (TestStr.Contains("Hell"))

{

    Console.WriteLine("包含!");

}

else

{

    Console.WriteLine("不包含!");

}

SubString:截取某段字符串

string TestStr = "Hello World";

Console.WriteLine(TestStr.Substring(0,5));//显示Hello

//第一个参数是起始位置,第二个参数是往后截取的位数(第二个参数可省略)

Console.WriteLine(TestStr.Substring(TestStr.Length-5));//显示World

//Length获得字符串的长度信息,这里直接用长度减5来实现输出字符串后五位的操作

IndexOf和LastIndexof:从前往后(Indexof)或从后往前(Last)找第一次出现的指定字符或字符串位置

string path = "E:\\学习\\dj\\C#课程\\day05\\课堂笔记.docx";

//为避免编译错误,规定类似斜杠这种特殊字符第一个斜杠为转义字符,第二个才是真正的特殊字符斜杠

int pos=path.LastIndexOf("\\");

int dotPos=path.LastIndexOf(".");

string fileName=path.Substring(pos+1);

Console.WriteLine(fileName);

//打印文件带后缀的全名

Console.WriteLine(fileName.Substring(0,dotPos-pos-1));//不带后缀的文件名

Console.WriteLine(path.Substring(dotPos+1));//文件类型

StartsWith和EndsWith:判断某字符串是否以某字符或字符串开始或结束

if(path.StartsWith("E:\\学习\\dj"))

{

    Console.WriteLine("属于DJ的项目的文件");

}

if(path.EndsWith(".docx"))

{

    Console.WriteLine("word文档");

}

Remove(移除指定字符或字符串)和Revserse(反转字符串)

string str = "&&屠龙宝刀点击就送$$";

Console.WriteLine(str.Remove(0,2));//显示屠龙宝刀点击就送$$

//第一个参数起始位置,第二个参数删除位数,可结合indexof等实现指定字符串删除.

Console.WriteLine(str.Reverse().ToArray());

//显示$$送就击点刀宝龙屠&&

Trim(删除字符串两端的空白格)和 Replace(替换某字符或字符串为指定字符或字符串,可以指定为空串完成remove的删除操作)

string str = "  屠龙宝刀点击就送  ";

Console.WriteLine(str+"空格后");//  屠龙宝刀点击就送  空格后

Console.WriteLine(str.Trim()); //"  屠龙宝刀点击就送  "

Console.WriteLine(str.Replace(" ",""));// "  屠龙宝刀点击就送  "

Concat:拼接字符串

string str1 = "我练功发自";

string str2 = "真心";

Console.WriteLine(string.Concat(str1,str2));//我练功发自真心

Join(以指定分隔符连接一个字符数组内各个字符)和Split(按选择的分隔符分离一个字符串并将其转为字符数组)

string[] names = { "张三","李四","王五" };

string str = "赵六-孙七-周八";

Console.WriteLine(string.Join("|",names));//张三|李四|王五

string[] namestr=str.Split('-');

foreach(string s in namestr)//单个依次输出赵六,孙七,周八

{

    Console.WriteLine(s);

}

2、实现身份证的解析;

Id.cs//实现对身份证号码的简单解析(只解析生日和性别)

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DJConsoleProject

{

    internal class Id

    {

        public void Identify(string id)

        {

            string Birth = GetBirth(id);

            string Gender = GetGender(id);

            Console.WriteLine("出生日期:" + Birth);

            Console.WriteLine("性别:"+Gender);

        }

        //获取生日

        public string GetBirth(string id)

        {

            string birthday = id.Substring(6, 8); // 身份证号中的出生日期部分

            string year = birthday.Substring(0, 4);

            string month = birthday.Substring(4, 2);

            string date = birthday.Substring(6, 2);

            string Bystr=year + month + date;

            return Bystr;

        }

        // 获取性别

        public string GetGender(string id)

        {

            string Gender = id.Substring(16, 1);//第17位奇数为男,偶数为女

            int G = int.Parse(Gender);//字符串转整数

            return G % 2 == 0 ? "女" : "男";//true为女,false为男

        }

    }

    

}

Program.cs//启动类

using DJConsoleProject;

using System;

using System.Security.Principal;

string idCardNum = "622922200306027013";

//网上随机查询的身份证号,联系立删。

if (idCardNum.Length != 18 || string.IsNullOrEmpty(idCardNum))

{

    Console.WriteLine("身份证号码错误!");

}

else

{

    Id id = new Id();

    id.Identify(idCardNum);

}

3、遍历磁盘,列出目录和文件、统计文件个数,统计不同类型(后缀)的文件个数。

DirectoryTraversal.cs//遍历目录处理所传的目录数据

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace DJConsoleProject

{

    public class DirectoryTraversal

    {

        public static int filecount=0;

        public static int[] poscount = new int[10];

        public static string[] posnames = new string[10];

        static public void TraverseDirectory(string path)

        {

            string[] files = Directory.GetFiles(path);

            Console.WriteLine(path+":");

            foreach (string file in files)

            {

                if (File.Exists(file))

                {

                    filecount++;

                    FileInfo fileInfo = new FileInfo(file);

                    string fileName = fileInfo.Name;

                    string fileExtension = fileInfo.Extension;

                    Console.WriteLine("文件名:"+fileName+",扩展名:"+fileExtension);

                    //if (string.IsNullOrEmpty(posnames[0]) && !fileExtension.Equals(posnames[0]))

                    //{

                    //    posnames[0] = fileExtension;

                    //    poscount[0]++;

                    //}

                    

                    for(int i=0;i<10;i++)

                    {

                        if (fileExtension.Equals(posnames[i])) { poscount[i]++; break; }

                        if (string.IsNullOrEmpty(posnames[i]) && !fileExtension.Equals(posnames[i]))

                        {

                            posnames[i] = fileExtension;

                            poscount[i]++;

                            break;

                        }

                    }

                }

                else { Console.WriteLine("File not exists!"); }

            }

            string[] dirs = Directory.GetDirectories(path);

            foreach (string dir in dirs)

            {

                if (Directory.Exists(dir))

                {

                    TraverseDirectory(dir);

                }

                else { Console.WriteLine("dir not exists!"); }

            }

        }

    }

}

Program//启动类

using DJConsoleProject;

using System;

using System.Security.Principal;

string directoryPath = "E:\\学习\\dj\\C#课程";

int[] count = DirectoryTraversal.poscount;

string[] posnames = DirectoryTraversal.posnames;

if(Directory.Exists(directoryPath))

{

    DirectoryTraversal.TraverseDirectory(directoryPath);

    Console.WriteLine("文件总数为:"+DirectoryTraversal.filecount);

    Console.WriteLine("不同类型文件后缀及个数");

    for(int i = 0; i < posnames.Length; i++)

    {

        if (!string.IsNullOrEmpty(posnames[i]))

            {

            Console.WriteLine("后缀:" + posnames[i] + "数量为:" + count[i]);

        }

        

    }

}

else

{

    Console.WriteLine("Directory does not exist.");

}

  • 21
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值