Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Whenever "IgnoreSymbols" is looking for ":" the return is = 5. Why?

C#
// OK. Returns 3.
int Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAA:AA", ":", System.Globalization.CompareOptions.IgnoreCase);

// OK. Returns 3.
Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAA:AA", ":", System.Globalization.CompareOptions.IgnoreNonSpace);

// BAD. Returns 5.
Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAA:AA", ":", System.Globalization.CompareOptions.IgnoreSymbols);

// OK. Returns 3.
Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAABAA", "B", System.Globalization.CompareOptions.IgnoreSymbols);

// OK. Returns 3.
Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAA:AA", ":", System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace);

// BAD. Returns 5.
Pos = System.Globalization.CultureInfo.CurrentCulture.CompareInfo.LastIndexOf("AAA:AA", ":", System.Globalization.CompareOptions.IgnoreCase | System.Globalization.CompareOptions.IgnoreNonSpace | System.Globalization.CompareOptions.IgnoreSymbols);


What I have tried:

in all the sites of internet.in all the sites of internet.
Posted
Updated 20-Sep-19 3:45am
v2

The documentation[^] is a little confusing - the overload you're calling simply says that it returns the index of the character, if found, or -1 otherwise.

However, looking at the other overloads, they include the additional information:
"Returns startIndex if value is an ignorable character."

Looking at the source of the overload you're calling[^], you can see it simply calls the other overload, passing in source.Length - 1 as the startIndex.

Since the value you're looking for is an ignorable character, the method therefore returns source.Length - 1 - ie: 5.

This seems like an odd implementation choice to me. I'd expect it to return -1 in this case. But it is at least correct according to the documentation. :)
 
Share this answer
 
Comments
[no name] 20-Sep-19 9:11am    
Very thankful. Where did you find that documentation? I have looked and I have not seen it.

I have also performed tests with Indexof and lastindexof indicated count (Character numbers) and always returns -1. But I have left because I do not use it.
Richard Deeming 20-Sep-19 9:22am    
I just put the type name into the .NET API Browser | Microsoft Docs[^].
Maciej Los 20-Sep-19 9:11am    
5ed!
If you know what the problem of the count parameter is, I also appreciate it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900