2

Make first letter of a string upper case (with maximum performance) suggests various methods for making only the first character of a string uppercase.

All of them though are essentially the equivalent of this:

char.ToUpperInvariant(st[0]) + str.Substring(1)

I don't know enough about the unicode spec to know if this is actually safe. Could you ever have a code point which is more than 1 char wide which could be capitalized?

Note that chars in C# are UTF16, and that ToUpperInvariant ultimately calls the operating system APIs to uppercase the char

Yair Halberstadt
  • 5,733
  • 28
  • 60
  • Your question boils down to "are there characters outside the Basic Multilingual Plane that could conceivably be capitalized", and the answer is "yes, but it's unlikely there's a need for it, and you'd need to set your own rules regardless as there are none under the invariant culture" (the latter is educated speculation on my part, though). If there were non-BMP characters that needed capitalization according to some culture, the string might change length, and you're looking at using `StringInfo.SubstringByTextElements` (and `String.ToUpper` with some custom culture). – Jeroen Mostert Mar 03 '21 at 16:08
  • Actually, there are 225 _small letters_ in the [Unicode database](https://www.unicode.org/Public/UNIDATA/UnicodeData.txt) above BMP. You could capitalize `str.Substring(0,2).ToUpperInvariant()` using [String.ToUpperInvariant Method](https://learn.microsoft.com/en-us/dotnet/api/system.string.toupperinvariant?view=net-5.0) providing that `char.IsHighSurrogate(str[0])`… – JosefZ Mar 03 '21 at 17:17
  • @JosefZ: nice find, though whether `.ToUpperInvariant` will touch those characters seems to vary, presumably by Unicode version/collation tables used by .NET/the underlying OS (details of which have varied across versions of both). – Jeroen Mostert Mar 03 '21 at 17:49

0 Answers0