zoukankan      html  css  js  c++  java
  • C#中字符串的操作

    1.Replace(替换字符):
    public string Replace(char oldChar,char newChar);在对象中寻找oldChar,如果寻找到,就用newChar将oldChar替换掉。
    如:
                string st = "abcdef";
                string newstring = st.Replace('a', 'x');
                Console.WriteLine(newstring);   //即:xbcdef

    public string Replace(string oldString,string newString);在对象中寻找oldString,如果寻找到,就用newString将oldString替换掉。
    如:
                string st = "abcdef";
                string newstring = st.Replace("abc", "xyz");
                Console.WriteLine(newstring);   //即:xyzdef


    2.Remove(删除字符):
    public string Remove(int startIndex);从startIndex位置开始,删除此位置后所有的字符(包括当前位置所指定的字符)。
    如:  
         string st = "abcdef";
                string newstring = st.Remove(4);
                Console.WriteLine(newstring);  //即:abcd

    public string Remove(int startIndex,int count);从startIndex位置开始,删除count个字符。
    如:  
         string st = "abcdef";
                string newstring = st.Remove(4,1);
                Console.WriteLine(newstring);  //即:abcdf


    3.Substring(字符串提取):
    public string Substring(int startIndex);从startIndex位置开始,提取此位置后所有的字符(包括当前位置所指定的字符)。
    如:  
         string st = "abcdef";
                string newstring = st.Substring(2);
                Console.WriteLine(newstring);  //即:cdef

    public string Substring(int startIndex,int count);从startIndex位置开始,提取count个字符。
    如:  
         string st = "abcdef";
                string newstring = st.Substring(2,2);
                Console.WriteLine(newstring);  //即:cd

    4.Trim(清空空格):
    public string Trim ():将字符串对象包含的字符串两边的空格去掉后返回。
    public string Trim ( params char[] trimChars ):从此实例的开始和末尾移除数组中指定的一组字符的所有匹配项。
    如:
         string st ="abcdef";
         string newstring = st.Trim(new char[] {'a'});//寻找st字符串中开始与末尾是否有与'a'匹配,如有,将其移除。
         Console.WriteLine(newstring); //即:bcdef
    注:如果字符串为"aaaabcdef",返回依然为bcdef。当移除第一个a时,开始依然为a,继续移除,直到没有。
    public string TrimEnd ( params char[] trimChars ):对此实例末尾与指定字符进行匹配,true则移除
    public string TrimStart ( params char[] trimChars ):对此实例开始与指定字符进行匹配,true则移除

    5.ToLower(转换大小写)

    public string ToLower():将字符串对象包含的字符串中的大写全部转换为小写。

    6.IndexOf(获取指定的字符串的开始索引)
    public int IndexOf (sring field):在此实例中寻找field,如果寻找到,返回开始索引,反之,返回-1。
    如:
           string st = "abcdef";
                int num=st.IndexOf("bcd");
                Console.WriteLine(num);  //即:1

    7.Equals(是否相等)
    public bool Equals (string value):比较调用方法的字符串对象包含字符串和参数给出的对象是否相同,如相同,就返回true,反之,返回false。
    如:        string a = "abcdef";
                bool b = a.Equals("bcdef");
                Console.WriteLine(b);//即:false

    public bool Equals ( string value, StringComparison comparisonType ):比较调用方法的字符串对象包含字符串和参数给出的对象是否在不区分大小写的情况下相同,如相同,就返回true,反之,返回false,第二个参数将指定区域性、大小写以及比较所用的排序规则.
    如:
           string a = "ABCDEF";
                bool b = a.Equals("abcdef",StringComparison.CurrentCultureIgnoreCase);
                Console.WriteLine(b);//即:true


    8.Split(拆分)
    public string[] Split ( params char[] separator ):根据separator 指定的没有字符分隔此实例中子字符串成为Unicode字符数组, separator可以是不包含分隔符的空数组或空引用。
    public string[] Split ( char[] separator, int count ):参数count 指定要返回的子字符串的最大数量。 
    如:
                string st = "语文|数学|英语|物理";
                string[] split = st.Split(new char[]{'|'},2);
                for (int i = 0; i < split.Length; i++)
                {
                    Console.WriteLine(split[i]);
                }
    注:count不填则全部拆分

    public enum StringSplitOptions 
    成员名称            说明
    None                返回值包括含有空字符串的数组元素
    RemoveEmptyEntries  返回值不包括含有空字符串的数组元素

    如:
                string st = "语文|数学||英语|物理";
                string[] split = st.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < split.Length; i++)
                {
                    Console.WriteLine(split[i]);
                }
    将StringSplitOptions枚举和Split()方法联系起来:
    1.  public string[] Split ( char[] separator, StringSplitOptions options ):options指定StringSplitOptions枚举的RemoveEmptyEntries以省略返回的数组中的空数组元素,或指定StringSplitOptions枚举的None以包含返回的数组中的空数组元
    2.  public string[] Split ( char[] separator, int count, StringSplitOptions options ) 
    3.  public string[] Split ( string[] separator, StringSplitOptions options ) 
    4.  public string[] Split ( string[] separator, int count, StringSplitOptions options )


    9.Contains(判断是否存在)
    public bool Contains(string text):如果字符串中出现text,则返回true,反之false,如果text为("")也返回true。
    如:
     string st="语文数学英语";
     bool b=st.Contains("语文");
     Console.WriteLine(b);//true


    10.EndsWith,StartsWith(判断字符串的开始或结束)
    public bool EndsWith ( string value ):判断对象包含字符串是否以value指定的字符串结束,是则为 true;否则为 false。 
    public bool EndsWith ( string value, StringComparison comparisonType ):第二个参数设置比较时区域、大小写和排序规则。
    public bool StartsWith ( string value ):判断对象包含字符串是否以value指定的字符串开始,是则为 true;否则为 false。 
    public bool StartsWith ( string value, StringComparison comparisonType ) :第二个参数设置比较时区域、大小写和排序规则。
    如:
     string st="语文数学英语abc";
     bool b=st.EndsWith("英语ABC",StringComparison.CurrentCultureIgnoreCase);//第二个参数忽略大小比较。
     Console.WriteLine(b);//true

    11.Insert(字符串插入)
    public string Insert ( int startIndex, string value ):在指定的字符串下标为startIndex前插入字符串value。返回插入后的值。
    如:
     string st="语文数学英语abc";
     string newst=st.Insert(6,"物理");//注:在指定索引“前”插入。
     Console.WriteLine(newst);//即:语文数学英语物理abc
     
    也不错。
    原文http://www.ffxun.com/content.aspx?id=48&domain=2

  • 相关阅读:
    bzoj1432_[ZJOI2009]Function
    Luogu1681_ 最大正方形II
    初等数论-Base-2(扩展欧几里得算法,同余,线性同余方程,(附:裴蜀定理的证明))
    [bzoj2456] mode
    初等数论-Base-1(筛法求素数,欧拉函数,欧几里得算法)
    小程序之Tab切换
    vue-axios基本用法
    vue-过渡动画
    vue-router实例
    永恒之蓝漏洞利用复现
  • 原文地址:https://www.cnblogs.com/net-sky/p/9235913.html
Copyright © 2011-2022 走看看