zoukankan      html  css  js  c++  java
  • NET 2.0中的字符串比较和方法

    .NET 2.0中的字符串比较

    平时编写代码的时候,字符串可能是大家接触得比较多的,所使用的字符串操作也应该得比较多的.在字符串比较方面,DOTNET2.0新增了一个枚举StringComparison

    namespace System
    {
          
    public enum StringComparison {
             CurrentCulture,
             CurrentCultureIgnoreCase,
             InvariantCulture,
             InvariantCultureIgnoreCase,
             Ordinal,
             OrdinalIgnoreCase
             }

    }

    可能大家平时在进行字符串比较的时候根本没有考虑那么多,"= =","Eaqual()"用得不亦乐乎,其实这里面的东西也挺多,性能,区域信息等
    1.首先是StringComparison.Ordinal        在进行调用String.Compare(string1,string2,StringComparison.Ordinal)的时候是进行非语言(non-linguistic)上的比较,API运行时将会对两个字符串进行byte级别的比较,因此这种比较是比较严格和准确的,并且在性能上也很好,一般通过StringComparison.Ordinal来进行比较比使用String.Compare(string1,string2)来比较要快10倍左右.(可以写一个简单的小程序验证,这个挺让我惊讶,因为平时使用String.Compare从来就没想过那么多).StringComparison.OrdinalIgnoreCase就是忽略大小写的比较,同样是byte级别的比较.性能稍弱于StringComparison.Ordinal.

    2.StringComparison.CurrentCulture        是在当前的区域信息下进行比较,这是String.Compare在没有指定StringComparison的时候默认的比较方式.例子如下:

     Thread.CurrentThread.CurrentCulture 
    = new CultureInfo("en-US"); //当前的区域信息是美国
                string s1 = "visualstudio";
                
    string s2 = "windows";
                Console.WriteLine(String.Compare(s1, s2,StringComparison.CurrentCulture)); 
    //输出"-1"

                Thread.CurrentThread.CurrentCulture 
    = new CultureInfo("sv-SE"); //当前的区域信息是瑞典
                Console.WriteLine(String.Compare(s1, s2,StringComparison.CurrentCulture)); //输出"1"StringComarison.CurrentCultureIgnoreCase指在当前区域信息下忽略大小写的比较.

    3.StringComarison.InvariantCulture        使用StringComarison.InvariantCulture来进行字符串比较,在任何系统中(不同的culture)比较都将得到相同的结果,他是使用CultureInfo.InvariantCulture的静态成员CompareInfo来进行比较操作的.例子如下:
                Thread.CurrentThread.CurrentCulture 
    = new CultureInfo("en-US"); //当前的区域信息是美国
                string s1 = "visualstudio";
                
    string s2 = "windows";
                Console.WriteLine(String.Compare(s1, s2,StringComparison.InvariantCulture)); 
    //输出"-1"

                Thread.CurrentThread.CurrentCulture 
    = new CultureInfo("sv-SE"); //当前的区域信息是瑞典
                Console.WriteLine(String.Compare(s1, s2,StringComparison.InvariantCulture)); //输出"-1"

    原始出处:http://www.cnblogs.com/cxd4321/archive/2007/03/08/667768.html (借鉴一下,谢谢)

    以下对C#中string类的方法进行汇总:

    1.string (char[])      使用指定的字符串数组构建一个新的string对象

    2.int Compare(string a,string b,bool case)     比较字符串a,b,case为true时表示不区分大小写。当a>b返回正数,当a<b返回负数,a=b返回0

    3. bool EndsWith(string)         确定当前字符串是否以指定的字符串结尾

    4. bool StartsWith(string)         确定当前字符串是否以指定的字符串开头

    5.int IndexOf()               返回指定的字符或字符串在当前字符串中的位置

    6.int  LastIndexOf()        返回指定字符或字符串的最后一个匹配项位置

    7.string Insert(int,string)         在当前的字符串中插入一个指定的字符串

    8.string Replace(string,string)   字符串替换

    9.string Remove(int,int)               从指定位置开始删除指定个数的字符

    10. ToUpper()    ToLower()           字符串大小写转换

    11.string  SubString(int,int)          返回从指定位置开始指定个数的字符串

  • 相关阅读:
    python 核心编程 第十三章
    pytho 核心编程 第十二章
    python 核心编程 第十章
    python 核心编程 第九章
    python核心编程 第八章
    python 核心编程第七章
    面试题总结(61-100)
    面试题总结(41-60)
    面试题总结(21-40)
    面试题总结(1-20)
  • 原文地址:https://www.cnblogs.com/aggierwyp/p/StringComparison.html
Copyright © 2011-2022 走看看