From 451380d2ad0dc249a9396974bbb6cd0cb1aa6546 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 2 Feb 2026 10:02:14 -0500 Subject: [PATCH] Simplify CultureInfo.CreateSpecificCulture with IndexOf --- .../src/System/Globalization/CultureInfo.cs | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs index c146f174ff1785..9903ab6307db4e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.cs @@ -277,25 +277,20 @@ public static CultureInfo CreateSpecificCulture(string name) // When CultureInfo throws this exception, it may be because someone passed the form // like "az-az" because it came out of an http accept lang. We should try a little // parsing to perhaps fall back to "az" here and use *it* to create the neutral. - culture = null; - for (int idx = 0; idx < name.Length; idx++) + int idx = name.IndexOf('-'); + if (idx >= 0) { - if ('-' == name[idx]) + try { - try - { - culture = new CultureInfo(name.Substring(0, idx)); - break; - } - catch (ArgumentException) - { - // throw the original exception so the name in the string will be right - throw; - } + culture = new CultureInfo(name.Substring(0, idx)); + } + catch (ArgumentException) + { + // throw the original exception so the name in the string will be right + throw; } } - - if (culture == null) + else { // nothing to save here; throw the original exception throw; @@ -1150,7 +1145,7 @@ private static Dictionary CachedCulturesByLcid public static CultureInfo GetCultureInfoByIetfLanguageTag(string name) { // Disallow old zh-CHT/zh-CHS names - if (name == "zh-CHT" || name == "zh-CHS") + if (name is "zh-CHT" or "zh-CHS") { throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_CultureIetfNotSupported, name)); }