c# - String.comparison performance (with trim) -
I need to do a lot of high-performance case-insensitive string comparison and I realized that way. ToLower () trim () is really stupid because all new strings are allocated
so I dug around a bit and looks better like this:
String Compare (txt1, The only problem here is that I want to ignore leading or trailing spaces, i.e. trim (), but if I use trim then i have the same problem with string allocation. I check every string I can see and can see that it starts with ("") or ends ("") and only then trim. Either it is or has indexed the index, pass the length and string for each string Compromise Override , Public Static Ent (String Stra, int indexA, String strb, int indexB, int length, StringComparison comparison type)
But it seems that I get disturbed and use some integer It does not matter if I can not really make it bigger - if more and more statements for each combination of trailing and leading spaces on both strings .. then any ideas of an elegant solution?
Here's my current proposal:
public bool ISEqual (string A, string b) {returns (string.compare (a, b, string comperson. ) == 0); } Public Bull ITRMAqual (String A, String B) {If (Math.Ebus (A.line-B.Line)) 2) // If the length is greater than 2, then can not be equal {return False; } And if (IsEqual (A, B)) {back true; } Other {Returns (string compiler (A.Rim), B.Rim (), String Compression, Ordinal Ignor Sees) == 0); }}
Something like this should be done:
Public Static Ent TrimCompareIgnoreCase (String A, String B) {int indexA = 0; Int indexB = 0; While (index A & L; Aline & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; lineback (while index b & lt; b lang & amp; amp; ; IV.ISHSiteSpace (B [Index B])) Index B ++; Int lenA = a.Length - indexA; Int lenB = B. Long-index B; while (lenA> 0 & amp; amp; Amp; amp; amp; amp; amp; amp; amp; amp ;;; all around; spacespace (an [indexA + lenA - 1])); lenA--; while (lenB> 0 & Amp; amp; amp; amp; amp; amp; amp; amp; amp; amp ;;; quarter (b [indexB + lenB - 1])) lenB--; if == 0 & amp; amp; amp; lenB == 0) if; (take == 0) returns 1; if (lenB == 0) Turn -1; Int result = String.Compare (A, index, B, indexb, meth.min (LNA, LNB, true); if (result == 0) {if (lenA & lt; lenB) results-- ; If (lenA> lenB) result ++;} return result;}
example:
string a = "asdf"; string b = "ASDF \ t"; console; light line (trim comport ignor case (A, B));
Output:
0
You need a profile against a simple trim and comparing it with some real data Not to see whether you are going to use it, there is no difference in fact to it.
Comments
Post a Comment