zoukankan      html  css  js  c++  java
  • C#字符串的方法

      1 static void Main(string[] args)
      2         {
      3             StrMethod();
      4         }
      5 
      6         public static void StrMethod()
      7         {
      8             string myString = "brambling";
      9             string myNewStrUP = string.Empty;
     10             string myNewStrLow = string.Empty;
     11             string myStringL = string.Empty;
     12             string myStringR = string.Empty;
     13 
     14             for (int i = 0; i < myString.Length; i++)       //如数组一样,可以通过 myString.Length 获取字符串的长度
     15             {
     16                 Console.WriteLine("字符:{0}", myString[i]);       //string 类型的变量可以看作是 char 类型变量的只读数组
     17             }                                                     //所以可以通过下标访问字符串的每一个字符(即元素)
     18 
     19 
     20             //但是,这种方式不能为各个字符赋值。为了获得一个可读可写的 char 数组,可以使用 myString.ToCharArray() 方法
     21 
     22 
     23             // myString.ToCharArray() 字符串转化为 Char[] 数组
     24             char[] myChar = myString.ToCharArray();
     25             for (int i = 0; i < myChar.Length; i++)     //如果要修改字符的值,就不能用 foreach 循环
     26             {   
     27                 if (i == 0)
     28                 {
     29                     myChar[i] = 'B';        //当i=0(即字符串的第一个字母),我们把它改为大写的 'B'
     30                 }
     31                 Console.WriteLine("字符:{0}", myChar[i]);
     32             }
     33 
     34 
     35             // myString.ToUpper() 把字符串转化为大写形式
     36             myNewStrUP = myString.ToUpper();
     37             Console.WriteLine("字符{0}的大写为:{1}", myString, myNewStrUP);       //输出结果:字符brambling的大写为:BRAMBLING
     38 
     39 
     40             // myString.ToLower() 把字符串转化为小写形式
     41             myNewStrLow = myNewStrUP.ToLower();
     42             Console.WriteLine("字符{0}的小写为:{1}", myNewStrUP, myNewStrLow);    //输出结果:字符BRAMBLING的小写为:brambling
     43 
     44 
     45             string myStr = "  str  ";
     46             string myNewStr = "---str---";
     47             string myStrL=string.Empty;
     48             string myStrR=string.Empty;
     49             string myNewStrL = string.Empty;
     50             string myNewStrR = string.Empty;
     51             Console.WriteLine("字符串‘  str  ’长度为:{0}", myStr.Length);      //为了比较好观察,用上面的方式获取字符串的长度,空格也是有长度的,结果为:7
     52 
     53 
     54             // myStr.TrimStart() 当没有参数的时候默认去掉左边的空格字符,有参数的时候去掉左边的指定字符
     55             myStrL = myStr.TrimStart();
     56             Console.WriteLine("字符串为:{0}", myStrL);
     57             Console.WriteLine("字符串长度为:{0}", myStrL.Length);      //去掉左边的空格,结果为:5
     58             myNewStrL = myNewStr.TrimStart('-');
     59             Console.WriteLine("字符串为:{0}", myNewStrL);    //字符串 "---str---" 去掉左边的字符 '-' 后的新字符串为:str---
     60 
     61 
     62             // myStr.TrimEnd() 当没有参数的时候默认去掉右边的空格字符,有参数的时候去掉右边的指定字符
     63             myStrR = myStr.TrimEnd();
     64             Console.WriteLine("字符串为:{0}", myStrR);
     65             Console.WriteLine("字符串长度为:{0}", myStrR.Length);      //去掉右边的空格,结果为:5
     66             myNewStrR = myNewStr.TrimEnd('-');
     67             Console.WriteLine("字符串为:{0}", myNewStrR);    //字符串 "---str---" 去掉右边的字符 '-' 后的新字符串为:---str
     68 
     69 
     70             //还有一个方法可以同时去掉左右两边的空格或指定字符
     71             // myStr.Trim() 当没有参数的时候默认去掉左右两边的空格字符,有参数的时候去掉左右两边的指定字符
     72             myStr = myStr.Trim();
     73             Console.WriteLine("字符串为:{0}", myStr);
     74             Console.WriteLine("字符串长度为:{0}", myStr.Length);      //去掉左右两边的空格,结果为:3
     75             myNewStr = myNewStr.Trim('-');
     76             Console.WriteLine("字符串为:{0}", myNewStr);    //字符串 "---str---" 去掉左右两边的字符 '-' 后的新字符串为:str
     77 
     78 
     79             //上面的方法是去掉空格字符或指定字符,下面的方法在字符串前后添加空格或指定字符
     80             /* myString.PadLeft(10) 当只有一个参数时,表示在字符串左边填充空格直至字符串长度为参数指定长度。
     81                                     当存在两个参数时,第一个的作用同一个参数时一样,第二个参数表示用指定的字符填充*/
     82             Console.WriteLine("字符串长度为:{0}", myString.Length);   //首先输出原字符串的长度:9
     83             myStringL = myString.PadLeft(10);       //在字符串前面添加空格,参数 10 表示指定填充后的新字符串长度
     84             Console.WriteLine("字符串长度为:{0}", myStringL.Length);  //输出填充后新字符串的长度为:10
     85             Console.WriteLine("字符串长度为:{0}", myString);
     86             Console.WriteLine("字符串长度为:{0}", myString.PadLeft(10, '-'));     //在字符串左边填充字符 '-' 使其长度达到 10
     87 
     88 
     89             // myString.PadRight(10) 使用方法同 myString.PadLeft(10) 只是一个作用于左边,一个作用于右边
     90             Console.WriteLine("字符串长度为:{0}", myString.PadRight(10, '-'));     //在字符串右边填充字符 '-' 使其长度达到 10
     91 
     92 
     93             // s.Split(',') 把字符串以指定字符分割为一个字符串数组
     94             string s = "one,two,three";
     95             string[] arrStr = s.Split(',');     //用 ',' 分割字符串,返回一个字符串数组
     96             for (int i = 0; i < arrStr.Length; i++)
     97             {
     98                 Console.WriteLine("字符串数组的第{0}个元素为:{1}", i, arrStr[i]);      //循环遍历字符串数组 arrStr 输出每一个字符串元素
     99             }
    100 
    101 
    102             // s.Replace(",", "-") 匹配第一个参数指定的字符(也可以是字符串),用第二个参数指定的字符(也可以是字符串)替换
    103             string sReplace = s.Replace(',', '-');
    104             Console.WriteLine("字符串{0}替换后的新字符串为:{1}", s, sReplace);
    105 
    106 
    107             // s.Insert(4, "four,") 第一个参数表示开始插入的起始位置,第二个参数表示要插入的字符串
    108             string sInsert = s.Insert(4, "four,");
    109             Console.WriteLine("字符串{0}插入后的新字符串为:{1}", s, sInsert); //输出结果为:one,four,two,three
    110 
    111 
    112             // s.Remove(3) 当只有一个参数时,表示删除从指定位置开始到最后所有的字符
    113             string sRemove = s.Remove(3);
    114             Console.WriteLine("字符串{0}删除后的新字符串为:{1}", s, sRemove); //输出结果为:one
    115             // s.Remove(4, 4) 当存在两个参数时,第一个参数表示开始的位置,第二个表示删除的字符数
    116             sRemove = s.Remove(4, 4);
    117             Console.WriteLine("字符串{0}删除后的新字符串为:{1}", s, sRemove); //输出结果为:one,three
    118 
    119 
    120             // StringBuilderl类,该类也具有 Insert()、Remove()、Replace()、AppendFormat() 方法,使用方法与 String 类的方法类似
    121             StringBuilder sb = new StringBuilder();
    122             sb.Append("one-two-three");
    123             sb.Append("-four");         //在字符串后面追加一个字符串
    124             Console.WriteLine(sb.ToString());    // sb.ToString() 把 StringBuilder 类型的字符串转换为 String 类型的字符串
    125 
    126 
    127             // String.Concat(arrStr) 为String类的静态方法,此方法具有其他重载
    128             Console.WriteLine(String.Concat(arrStr));   //将字符串数组连接为一个字符串,输出结果为:onetwothree
    129 
    130 
    131             // String.Join(",", arrStr) 为String类的静态方法,第一个参数为指定分割符号,第二个参数为一个字符串数组,此方法具有其他重载
    132             Console.WriteLine(String.Join(",", arrStr));    //以指定分割符号将一个字符串数组连接成一个字符串,输出结果为:one,two,three
    133 
    134 
    135             // s.IndexOf(",") 在字符串中查找指定字符或字符串第一次出现的位置,返回其索引值,如果未查找到,则返回 -1 
    136             Console.WriteLine("第一个逗号出现的位置为:" + s.IndexOf(","));     //输出结果为:3
    137 
    138 
    139             // s.LastIndexOf(",") 在字符串中查找指定字符或字符串最后一次出现的位置,返回其索引值,如果未查找到,则返回 -1 
    140             Console.WriteLine("最后一个逗号出现的位置为:" + s.LastIndexOf(","));        //输出结果为:7
    141 
    142 
    143             // s.Substring(4) 此方法具有一个重载,当只有一个参数时为指定开始截取的位置,一直截取到字符串的末尾
    144             //当为两个参数时,第一个为指定截取字符串的起始位置,第二个为指定截取字符串的长度
    145             Console.WriteLine("未指定截取长度的字符串为:" + s.Substring(4));        //输出结果为:two,three
    146             string st = s.Substring(s.IndexOf(",") + 1, s.LastIndexOf(",") - (s.IndexOf(",") + 1));
    147             Console.WriteLine("指定截取长度的字符串为:" + st);         //输出结果为:two
    148 
    149 
    150             // String.Format() 为String类的静态方法,其作用是格式化字符串,此方法具有其他重载
    151             Console.WriteLine(String.Format("{0} + {1} = {2}", 1, 2, 1 + 2));
    152             Console.WriteLine(String.Format("{0} / {1} = {2:0.000}", 1, 3, 1.00 / 3.00));       //保留三位小数
    153             Console.WriteLine(String.Format("{0:yyyy年MM月dd日}", DateTime.Now));
    154 
    155 
    156             Console.ReadKey();
    157         }
  • 相关阅读:
    Struts2+Spring3+Mybatis3开发环境搭建
    spring+struts2+mybatis
    【LeetCode】Populating Next Right Pointers in Each Node
    【LeetCode】Remove Duplicates from Sorted Array
    【LeetCode】Remove Duplicates from Sorted Array II
    【LeetCode】Binary Tree Inorder Traversal
    【LeetCode】Merge Two Sorted Lists
    【LeetCode】Reverse Integer
    【LeetCode】Same Tree
    【LeetCode】Maximum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/Brambling/p/5994460.html
Copyright © 2011-2022 走看看