zoukankan      html  css  js  c++  java
  • 深入C# String类

    深入C# String类

    C#中的String类

    他是专门处理字符串的(String),他在System的命名空间下,在C#中我们使用的是string

    小写的string只是大写的String的一个别名(外号)使用大写和小写都是一样的

    常用的字符串处理法

    Java中常用的字符串处理方法?

    1)  IndexOf:字符串的检索

    a)         IndexOf(String s):重头开始检索

    b)         IndexOf(String s,startString s):从startString开始检索

    2)  获取长度:.length()

    3)  判断.equals(参数)

    4)  得到字符串的子字符

    subString(参数)

    subString(参数1,参数2)

     

    C#提供的字符串比较的方法

    1)  IndexOf:查找某个字符在字符串当中的位置

    2)  subString:从字符串中截取子字符

    3)  ToLower():转换成小写

    4)  ToUpper():大写

    5)  Time():去空格

    6)  Equals():比较字符串值

    If(name == “”)                    地址

    If(name.Equals(String.Empty))值

    “”和Empty的区别?

    If(name == “”)                              分配一个长度为空的存储空间

    If(name.Equals(String.Empty)) 不会分配存储空间

             判断空字符串的三种写法?这三个的性能比较?

    Name.Length ==0                      2

    Name == String.Empty              1

    Name == “”                                  3

    7)  joi():链接字符串

    8)  split():分割

    获取邮箱用户名

             需求:获取邮箱的用户名

                         兼用各种格式yes(YES)

                         循环执行

    public void GetUserName()

    {

        string code;//用户选择

        do{

            this.PickNameFoemEmail();

            Console.WriteLine("是否继续?yes/no");

            code = Console.ReadLine();

            code = code.Trim().ToLower();

        }while(code.Equals("yes"));

    }

    public void PickNameFoemEmail()

    {

        string emsil;// 获取邮箱

        string name;//获取用户名

     

        Console.WriteLine("请输入邮箱:");

        emsil = Console.ReadLine();

     

        Console.WriteLine("你的邮箱是{0}:",emsil);

     

     

        // 提取

        int posion = emsil.IndexOf("@");

        if (posion > 0)

        {

            name = emsil.Substring(0, posion);

     

            Console.WriteLine("你的邮箱地址是:{0}", name);

        }

        else

        {

            Console.WriteLine("你的邮箱格式错误");

        }

    }

    Class1 c = new Class1();

    c.GetUserName();

     

    Console.ReadKey();

                           

    连接分割字符串

    Join     split

    // 输入的字符串

    string inputString;

    // 分割后的字符串数组

    string[] splitString;

    // 连接后的

    string joinString;

     

    Console.WriteLine("请输入字符串,用空分开:");

     

    inputString = Console.ReadLine();

    splitString = inputString.Split(' ');

    Console.WriteLine(@" 分割后的:");

    foreach (var item in splitString)

    {

        Console.WriteLine(item);

    }

    joinString = string.Join("+连接+",splitString);

     

    Console.WriteLine(" 连接后的字符串:{0}",joinString);

    @” 转义符”:忽略掉

    Format格式化(不是清除的意思)

    String name = “Tom”;

    Console.WritrLine(“我的名字:{0},我的年龄{1}”,name,22);

    {x}占位符的方式去输出

    string name;

                string birthday;

                int height;

                string bloodType;

                string planet;

                string loveFood;

                string record;

     

                Console.WriteLine("欢迎来到“C#”的世界!");

                Console.WriteLine("请输入你的个人信息,我将为你建立个人档案:");

                Console.Write("姓名:");

                name = Console.ReadLine();

                Console.Write("出生年月:(*年*月*日):");

                birthday = Console.ReadLine();

                Console.Write("身高(cm):");

                height = int.Parse(Console.ReadLine());

                Console.Write("星座:");

                planet = Console.ReadLine();

                Console.Write("血型:");

                bloodType = Console.ReadLine();

                Console.Write("喜欢的食物:");

                loveFood = Console.ReadLine();

     

                record = string.Format("姓名:{0} 出生年月:{1} 身高:{2} 星座:{3} 血型:{4} 喜欢的食物:{5}",name,birthday,height,bloodType,planet,loveFood);

     

                Console.WriteLine(" 这是你的个人档案:");

                Console.WriteLine(record);

     

                Console.ReadKey();

    Grammar:String myString = string.Format(“格式化字符串”,参数列表)

    2 * 3 = 6

    String myString = string.Format(“{0}乘以{1}等于{2}”,2,3 ,2 * 3);

    如输出货币

    语法:

    格式字符串包括:固定文本和格式项

    格式项

    Console.WriteLine("{0}",50);

                Console.WriteLine(String.Format("{0,-8:F2}",50));

                Console.WriteLine(String.Format("{0,8:C2}", 50));

    Format()方法的格式化字符串中各种格式化定义符和示例

    1)  C:货币格式

    2)  D十进制格式

    3)  F小数点后固定位数

    4)  用逗号隔开的数字

    5)  百分比计数法

    6)  十六进制格式

    Console.WriteLine("{0}",String.Format("{0:C3}",3000));

    Console.WriteLine("{0}",String.Format("{0:D3}",2000));

    Console.WriteLine("{0}", String.Format("{0:F3}", 2000));

    Console.WriteLine("{0}", String.Format("{0:N}", 230000));

    Console.WriteLine("{0}", String.Format("{0:P3}", 0.921867357621));

    Console.WriteLine("{0}", String.Format("{0:X000}", 12));

  • 相关阅读:
    编码导致 html和aspx 样式差异,变形
    Recommand of the Day:Names in English
    NSBundle常用方法及解释
    在K8S 中部署 Spring Boot 应用,爽!
    如果抛开 Spring,如何自己实现 AOP?面试必问。。。
    为什么阿里强制 boolean 类型变量不能使用 is 开头?
    CTO 说禁用 Lombok,看我怼死他。。
    面试官:什么是 YAML?和 Spring Boot 有什么关系?
    面试官:线程池多余的线程是如何回收的?
    JetBrains 发布下一代 IDE,无比轻量,几秒就能启动干活,IDEA 可以扔了。。
  • 原文地址:https://www.cnblogs.com/danxun/p/10081007.html
Copyright © 2011-2022 走看看