zoukankan      html  css  js  c++  java
  • c# 中的 Trim

    1. 让用户输入字符串 并且判断是否是 'yes'(无关大小写)

                  Console.WriteLine("input a string");
                  string userResponse = Console.ReadLine();
                 if (userResponse.ToLower() == "yes")
                 {
                     Console.WriteLine("you typed yes");  
                 //act respons
                 }
    

    2.如果用户输入的字符串前后不小心加了空格, 这时候怎么办(注意是前后不包括中间)

                 Console.WriteLine("input a string");
                string userResp = Console.ReadLine();
               /* userResp = userResp.Trim();  // Trim 可以删除字符串前面后者后面的空格
                if(userResp.ToLower()=="yes")
                {
                    Console.WriteLine("how about yes ");
                }
    

    3. 如果用户输入的字符串最后有e,s 我们要怎么办

     Console.WriteLine("input a string");
                string userResp = Console.ReadLine();
                char[] myChar={' ', 'e','s'};
               /* userResp = userResp.Trim(myChar);  // Trim 可以删除字符串前面后者后面的空格
                if(userResp.ToLower()=="yes")
                {
                    Console.WriteLine("how about yes ");
                }
    

      重点: Trim 只能删除前后的字符

    4.如何对齐?

     Console.WriteLine("补齐:");
                Console.WriteLine("小于10个字符");
                string myString =Console.ReadLine();
                myString = myString.PadLeft(10);// left左补齐 用空格
                string myString1 = myString.PadLeft(20, 'a'); //如果这里写 string myString1 = myString.PadLeft(10, 'a'); 会怎样
    
                Console.WriteLine(myString);
                Console.WriteLine(myString1);
                Console.ReadKey();
    

    5. Split

    string myStringThere = "This is a test.";
                    char [] separator ={' '};
                string [] myWords;
                myWords = myStringThere.Split(separator);
                foreach (string word in myWords)
                {
                    Console.WriteLine("{0}", word);
                }
                Console.ReadKey();
    

      

  • 相关阅读:
    矩阵乘法(二):利用矩阵快速幂运算完成递推
    更改codeblock编译后程序的图标
    如何在VS2008下使用FLTK
    Python type() 函数
    Python range() 函数用法
    Python len()方法
    Python filter() 函数
    Python bool() 函数
    数据类型
    JAVA标识符
  • 原文地址:https://www.cnblogs.com/yoyov5123/p/3325452.html
Copyright © 2011-2022 走看看