In C#, I am using
CultureInfo.GetCultureInfo(myCulture)
but the string variable may not come in a good format, is there a way to try parse the string first or verify it first.
In C#, I am using
CultureInfo.GetCultureInfo(myCulture)
but the string variable may not come in a good format, is there a way to try parse the string first or verify it first.
The following yields a collection of all cultures:
CultureInfo.GetCultures(CultureTypes.AllCultures)
From there, rather than GetCultureInfo
you could do:
.FirstOrDefault(c => c.Name == myCulture)
Rather than AllCultures
you may want to filter out SpecificCultures
.
Dictionary<,>
if this is something you do all the time. As in: static readonly Dictionary<string, CultureInfo> CultureDict = CultureInfo.GetCultures(CultureTypes.AllCultures).ToDictionary(c => c.Name);
–
Derwent there is no tryparse with culture objects. one way is to go through all cultures as suggested and look for one and the other way is to use the simple try parse:
try
{
// making sure the lang is a calture
System.Globalization.CultureInfo c = new System.Globalization.CultureInfo(lang);
}
catch
{
lang = Session["lang"].ToString();
}
catch
to catch(CultureNotFoundException ex)
. –
Labourer I always use a little helper in my project. All arithmetic types got TryParse method
public static bool TryParseDouble(this string text, out double value)
{
return double.TryParse(text, NumberStyles.Any,
CultureInfo.InvariantCulture, out value);
}
The usage
double value;
bool isStringOK = theString.TryParseDouble(out value);
double
have to do with CultureInfo
as the original poster asks? –
Delete © 2022 - 2024 — McMap. All rights reserved.
int
– Bratwurststring
into aCultureInfo
. – Delete