Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String.Equals on User Input

I’ve read and re-read https://docs.microsoft.com/en-us/dotnet/csharp/how-to/compare-strings and https://docs.microsoft.com/en-us/dotnet/standard/base-types/best-practices-strings and I am still unclear on one thing: what comparison type should I use for user inputted strings.

IE, let’s say I have the string in a db record that supports unicode, and before running an update query on the database, I want to make sure the string has actually changed so I do if (string.Equals(dbstring, userinput, StringComparison.?)) { // update db. }

So which one do I use? Reading the guides above, I primarily use StringComparison.CurrentCulture for UI display such as sorting, and I use Ordinal most things under the hood, and I should rarely use InvariantCulture.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The part that is confusing me is this line (emphasis mine):

Do not use string operations based on
StringComparison.InvariantCulture in most cases. One of the few
exceptions is when you are persisting linguistically meaningful but
culturally agnostic data.

What do they mean ‘persisting’? Does this apply to the case of storing unicode strings of user input in a database?

>Solution :

"Persisting" in most cases means "storing". Just that. Fancy word for simple thing.

It doesn’t really matter if it’s database storage, file storage, internet cloud storage, or, yes, even in-memory storage — even though "in-memory" doesn’t usually coincide with thinking about storage, since it tends to evaporate when the power goes out. It’s more about how you are going to use this data, rather than where you are going to store it. So even building a in-memory most-recently-used list of terms can be thought as "persisting", if it’s kept long enough in a typical non-disastrous case.

So, yes, storing unicode strings in a database is exactly as good use case for the word "persisting" as it can be.

However, for your use-case of determining whever the text entered by the user has changed, I’m not sure if you should focus on the ‘persisting’ aspect. At this point I think you don’t really care about storing, you care about "has it been changed", and that should determine the choice of string comparison.

All StringComparison flags have some effects. Culture vs Invariant, Case-Sensitive or not, how would you like the comparison to behave, so that the result will be clear and understandable to the user?

If old text was "Mary has a lamb" and new text is "mAry HaS a Lamb", should it be treated as a change?

If old text was "Maria hat ein weißes Lamm" and new text is "Maria hat ein weisses Lamm", should it be treated as a change?

and so on. Right now I can’t think of better examples, I’m sure you could think of some when focusing on your userbase and on what will be of the most use to them.

Please note, that they also may not care, and that you may be overthinking it or focusing on too tiny details too soon. Maybe the default comparison would be just fine for first few years until users tell you it could be better here and there? Dunno. YMMV 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading