本文介绍了String.comparison性能(带装饰)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做的高性能不区分大小写字符串比较的很多,并意识到我做.ToLower方式()。修剪()是非常愚蠢的,由于做被分配所有的新字符串



所以,我周围挖一点,这样似乎更可取:

 的String.Compare(TXT1 ,TXT2,StringComparison.OrdinalIgnoreCase)

这里唯一的问题是,我想忽略前导或尾随空格即修剪(),但如果我使用修剪我有串分配同样的问题。我想我可以检查每个字符串,看看它是否StartsWith()或的endsWith(),然后才修剪。如果不是这样,计算出指数,长度每串并传递到的String.Compare控

 公共静态INT比较

串STRA,
INT索引A,
串STRB,
INT indexB,
INT长度,
StringComparison comparisonType

但似乎相当混乱,我可能有,如果我不要让一个真正的大到如果 - 使用一些整数?拖尾和两个字符串的前导空白......所以一个优雅的解决方案的任何想法的每个组合else语句



下面是我目前的建议:

 公共BOOL ISEQUAL(字符串,字符串b)
{
回报率(的String.Compare(A,b,StringComparison.OrdinalIgnoreCase )== 0);
}

公共BOOL IsTrimEqual(字符串,字符串B)
{
如果(Math.Abs​​(a.Length- b.length个)→2) //如果长度超过2不同,不能等于
{
返回FALSE;
}
,否则如果(ISEQUAL(A,B))
{
返回真;
}
,否则
{
回报率(的String.Compare(a.Trim(),b.Trim(),StringComparison.OrdinalIgnoreCase)== 0);
}
}


解决方案

东西像这样应该这样做:

 公共静态INT TrimCompareIgnoreCase(字符串,字符串二){
INT索引A = 0 ;
INT indexB = 0;
,而(索引A<&则为a.length功放;&安培; Char.IsWhiteSpace(A [索引A))索引A ++;
,而(indexB<​​;&b.length个放大器;&安培; Char.IsWhiteSpace(B [indexB))indexB ++;
INT LENA =则为a.length - 索引A;
INT LENB = b.length个 - indexB;
,而(莉娜大于0和放大器;&安培; Char.IsWhiteSpace(A [索引A + LENA - 1]))lenA--;
而(LENB大于0&放大器;&放大器; Char.IsWhiteSpace(二[indexB + LENB - 1]))lenB--;
如果(莉娜== 0安培;&安培; LENB == 0)返回0;
如果(莉娜== 0)返回1;
如果(LENB == 0)返回-1;
INT结果=的String.Compare(一,索引A,B,indexB,Math.Min(LENA,LENB),TRUE);
如果(结果== 0){
如果(莉娜< LENB)result--;
如果(莉娜> LENB)结果++;
}
返回结果;
}



例如:

 字符串=ASDF; 
字符串B =ASDF \t

Console.WriteLine(TrimCompareIgnoreCase(A,B));



输出:

  0 

您应该简介这对一个简单的修剪,并与一些真实的数据进行比较,看看是否有真的是你要使用它什么任何区别。


I need to do alot of high-performance case-insensitive string comparisons and realized that my way of doing it .ToLower().Trim() was really stupid due do all the new strings being allocated

So I digged around a little and this way seems preferable:

String.Compare(txt1,txt2, StringComparison.OrdinalIgnoreCase)

The only problem here is that I want to ignore leading or trailing spaces, ie Trim() but if I use Trim I have the same problem with string allocations. I guess I could check each string and see if it StartsWith(" ") or EndsWith(" ") and only then Trim. Either that or figure out the index,length for each string and pass to string.Compare override

public static int Compare
(
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    StringComparison comparisonType
)

but that seems rather messy and I probably have to to use some integers if I dont make a really big if-else statement for every combination of trailing and leading blanks on both strings... so any ideas of an elegant solution?

Here's my current proposal:

public bool IsEqual(string a, string b)
    {
        return (string.Compare(a, b, StringComparison.OrdinalIgnoreCase) == 0);
    }

    public bool IsTrimEqual(string a, string b)
    {
        if (Math.Abs(a.Length- b.Length) > 2 ) // if length differs by more than 2, cant be equal
        {
            return  false;
        }
        else if (IsEqual(a,b))
        {
            return true;
        }
        else 
        {
            return (string.Compare(a.Trim(), b.Trim(), StringComparison.OrdinalIgnoreCase) == 0);
        }
    }
解决方案

Something like this should do it:

public static int TrimCompareIgnoreCase(string a, string b) {
   int indexA = 0;
   int indexB = 0;
   while (indexA < a.Length && Char.IsWhiteSpace(a[indexA])) indexA++;
   while (indexB < b.Length && Char.IsWhiteSpace(b[indexB])) indexB++;
   int lenA = a.Length - indexA;
   int lenB = b.Length - indexB;
   while (lenA > 0 && Char.IsWhiteSpace(a[indexA + lenA - 1])) lenA--;
   while (lenB > 0 && Char.IsWhiteSpace(b[indexB + lenB - 1])) lenB--;
   if (lenA == 0 && lenB == 0) return 0;
   if (lenA == 0) return 1;
   if (lenB == 0) return -1;
   int result = String.Compare(a, indexA, b, indexB, Math.Min(lenA, lenB), true);
   if (result == 0) {
      if (lenA < lenB) result--;
      if (lenA > lenB) result++;
   }
   return result;
}

Example:

string a = "  asdf ";
string b = " ASDF \t   ";

Console.WriteLine(TrimCompareIgnoreCase(a, b));

Output:

0

You should profile it against a simple Trim and Compare with some real data, to see if there really is any difference for what you are going to use it for.

这篇关于String.comparison性能(带装饰)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:54