How to Get the Length of a String in C#
Length tells you how many characters a string has: "abc".Length is 3. It's what you check when a value must be at least, or at most, a certain number of characters long.
General Programming Concepts and Languages
View All TagsLength tells you how many characters a string has: "abc".Length is 3. It's what you check when a value must be at least, or at most, a certain number of characters long.
In a normal C# string, a backslash starts an escape sequence: \n is a line break. Put @ in front of the string and a backslash is just a backslash, so @"C:\Reports\2026" means exactly what it shows.
char.IsLetter answers one question about one character: is it a letter? It knows every alphabet, so é and Ж count, which is usually what you want, and sometimes isn't.
Files, network requests and hashes work with bytes, not strings. Encoding.UTF8.GetBytes turns a string into UTF-8 bytes, and Encoding.UTF8.GetString turns those bytes back into the same text.
TrimEnd removes characters from the end of a string: the ones you list, as many as are there, until it reaches one that isn't on the list. It's the quick way to take trailing punctuation, slashes or spaces off a value.
Text that people type often has spaces they didn't mean to type, at its start or its end. Trim removes them, together with tabs and line breaks, and leaves the text in between alone.
To turn a list into one line of text, such as Tokyo, London, New York, use string.Join. It puts the separator only between items, so there's never a stray comma at the end.
The quickest way to put strings together in C# is the + operator: firstName + " " + lastName. When one side is a string, the other side is turned into text too, which is handy, and once in a while surprising.
To treat Tokyo, TOKYO and tokyo as the same answer, turn them into lowercase first. ToLowerInvariant does that with the same rules on every computer, so the result doesn't depend on the language the computer is set to.
A small value, such as a temperature reading or a point on a map, needs properties, a constructor, equality and a ToString before it's pleasant to use. A readonly record struct gets all of that from one line, and its properties can't change once it's created.