zoukankan      html  css  js  c++  java
  • 字符串(String)几种处理方式

      1 int[][] sdf = new int[1][];
      2 sdf[0] = new int[2];//第一行实例化列并给它声明2个位置
      3 sdf[0][0] = 2;
      4 sdf[0][1] = 5;
      5 Console.WriteLine(sdf[0][0]);
      6 
      7 // 一、比较字符串
      8 
      9 // 1.使用compare方法
     10 
     11 //int Compare(string A,string B)比较字符串A和B是否相等
     12 //int Compare(string A,string B,bool ignorCase)第三个参数表示是否忽略大小写,true表示忽略
     13 
     14 string str1 = "一念月初";// 初的拼音开头为 c
     15 string str2 = "一念月末";//末的拼音开头为 m
     16 //c<m -1
     17 //m>c 1
     18 
     19 Console.WriteLine(string.Compare(str1, str2));//-1,如果前面一个较小,返回-1
     20 Console.WriteLine(string.Compare(str2, str1));//1,如果前面一个较大,返回1
     21 Console.WriteLine(string.Compare(str1, str1));//0,如果两个字符串相等 返回0
     22 
     23 //2.CompareTo 方法
     24 
     25 Console.WriteLine(str1.CompareTo(str2));//-1
     26 
     27 //3.Eauals方法 ,返回True或False
     28 
     29 //方法一
     30 Console.WriteLine(str1.Equals(str2));//比较两个字符串是否相等,相等返回True
     31 //方法二
     32 Console.WriteLine(string.Equals(str1, str2));
     33 
     34  
     35 
     36 //格式化字符串
     37 
     38 string info = string.Format("{0}现在就读于{1}班", "tom", "yc37");
     39 Console.WriteLine(info);
     40 
     41 //还可以用来格式化时间样式
     42 
     43 DateTime dt = DateTime.Now;//获取系统当前时间
     44 string time = string.Format("{0:D}", dt); //{0}表示占位符 D为样式
     45 
     46 //自定义格式
     47 
     48 time = string.Format("{0:yyyy年MM月dd日 HH:mm:ss}", dt);//MM表示月份,mm表示分钟 HH为24小时制 hh为12小时制
     49 Console.WriteLine(time);
     50 
     51 
     52 //截取字符串
     53 Console.WriteLine(str1.Substring(1, 4));//一念月初 从索引号为1的字符开始往后截取4个字符
     54 Console.WriteLine(str2.Substring(1));//一念月末 从索引号为1的字符开始往后截取到字符串末尾
     55 
     56  
     57 
     58 //分割字符串
     59 //split 参数是char,不是string
     60 
     61 //二、复制字符串
     62 //1.Copy方法 public static string Copy(string str)
     63 
     64 string s = string.Copy(str1);
     65 Console.WriteLine(s);
     66 
     67 //2.CopyTo方法
     68 //void CopyTo(int sourceIndex,//从源字符串第几个字符开始copy,第一个为0
     69 //char[] destination,//目标字符串的char数组
     70 //int destinationIndex,//从目标字符串char数组的第几个位置开始放
     71 //int count//一共复制多少个字符
     72 //)
     73 
     74 char[] ch = new char[100];
     75 
     76 //将字符串str1从索引1的位置开始的4个字符串复制到ch中
     77 str1.CopyTo(1, ch, 0, 4);
     78 Console.WriteLine(ch);
     79 
     80 
     81 //三、替换字符串
     82 //public string Replace(char oldar,char newchar)
     83 //public string Replace(string oldstring,string new string)
     84 
     85 string a = "one world,one dream";
     86 //使用“*”替换“,”,并且赋值给b
     87 string b = a.Replace(',', '*');
     88 //使用“*”代替“o”,当原始字符串中存在多个o时,会全部被替换
     89 Console.WriteLine(b);
     90 
     91 
     92 //使用“One world ”替换"one world"
     93 string c = a.Replace("one world", "One world");
     94 Console.WriteLine(c);
     95 
     96 
     97 //string
     98 
     99 //字符串一旦声明就不再可以改变,如果要替换里面的值,要将替换了的放到一个新的字符串中
    100 //例子:
    101 string ab = "dfsdfsdf";
    102 
    103 ab.Replace("d", "m");
    104 Console.WriteLine(ab);//结果还是原来那个,因为字符串一旦声明就不可改变了
    105 
    106 string de = ab.Replace("d", "m");
    107 Console.WriteLine(de);//结果变了,因为它把改变的放到了新的一个字符串中
    108 //
    109 ab = ab.Replace("d", "m"); //直接替换掉原来的内容
  • 相关阅读:
    宿主机无法访问CentOS7上Jenkins服务的解决办法
    415. Add Strings
    367. Valid Perfect Square
    326. Power of Three
    258. Add Digits
    231. Power of Two
    204. Count Primes
    202. Happy Number
    172. Factorial Trailing Zeroes
    171. Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/Dream-High/p/3383858.html
Copyright © 2011-2022 走看看