zoukankan      html  css  js  c++  java
  • 3月5日 String类

    1.Length    --取字符串长度

    string x = Console.ReadLine();
                int i = x.Length;
                Console.WriteLine(i);

    执行结果:

    1

    2.Substring(从哪一位开始,截取几位)   --截取字符串,从第六位往后截取3位,包括空格在内

    string x = Console.ReadLine();
               string p = x.Substring(6, 3);
               Console.WriteLine(p);

    执行结果:

    3

    (1) 取字符串的前i个字符  

    str=str.Substring(0,i); // 或  str=str.Remove(i,str.Length-i);

    (2) 去掉字符串的前i个字符:

      str=str.Remove(0,i);  // 或 str=str.Substring(i);

    (3)从右边开始取i个字符:

     str=str.Substring(str.Length-i); // 或 str=str.Remove(0,str.Length-i);

    (4)从右边开始去掉i个字符:  

    str=str.Substring(0,str.Length-i); // 或str=str.Remove(str.Length-i,i);

    (5)截取字符串最后一个字符的问题

    str1.Substring(str1.LastIndexOf(",")+1);

    或k = k.Substring(k.Length-1, 1);

    3.Trim()  --压缩前后空格     TrimStart():压缩前面空格    TrimEnd():压缩后面空格

    string x = Console.ReadLine();
             int i = x.Length;
             Console.WriteLine(i);

             string s = x.Trim();
             Console.WriteLine(s);

             int p = s.Length;
             Console.WriteLine(p);

    执行结果:

    9

    4.ToLower()所有大写都转成小写

    ToUpper()所有小写都转成大写

    string x = Console.ReadLine();
           x = x.ToLower();
           Console.WriteLine(x);
           x = x.ToUpper();
           Console.WriteLine(x);

    执行结果:

    0

    5.Replace("被替换的","替换为")  --替换字符  (前面是被替换的,后面是替换成的)

        string x = Console.ReadLine();
              string p = x.Replace("a", "A");
              Console.WriteLine(p);

    执行结果:

    QQ截图20150305152322

    6.Remove(从哪一位开始,删除几位)   --删除字符

    string x = Console.ReadLine();
                x=x.Remove(5,3);
                Console.WriteLine(x);

    执行结果:

    115

    7.Split(Char[])    --分隔字符串

    string words = "This is a list of words, with: a bit of punctuation" +
                         " and a tab character.";
                string[] str = words.Split(new Char[] { ' ', ',', '.', ':', ' ' });
                foreach (string s in str)
                {
                    if (s.Trim() != "")
                        Console.WriteLine(s);
                }

    执行结果:

    4

  • 相关阅读:
    日志服务器 03-部署日志服务器(网络设备部分)
    日志服务器 02-部署日志服务器(主机部分)
    日志服务器 01-Linux日志系统syslog
    nyoj--523--亡命逃窜(BFS水题)
    hdoj--3488--Tour(KM)
    hdoj--1533--Going Home(KM)
    hdoj--2255--奔小康赚大钱(KM算法模板)
    LightOJ--1152--Hiding Gold(二分图奇偶建图)(好题)
    hdoj--5093--Battle ships(二分图经典建图)
    LightOJ--1149--Factors and Multiples(二分图好题)
  • 原文地址:https://www.cnblogs.com/tzq9308/p/4316274.html
Copyright © 2011-2022 走看看