What's the most efficient way to pull culture information from a resx filename in C #?

What's the most efficient way to output culture information from a resx filename using C #? The solution is also to handle there no culture information in the filename (i.e. form1.resx). In this case, the default string should be returned.

+2


a source to share


2 answers


This seems to work:



string GetCulture(string s)
{
  var arr=s.Split(".");
  if(arr.Length>2)
    return arr[1];
  else
    return "Default";
}

      

+4


a source


Your best bet is to use regex:

string ParseCulture(string input)
{
  var r = Regex.new('[\w\d]+\.([\w\-]+)\.resx')
  // Match the regular expression pattern against a text string.
  Match m = r.Match(input);
  if (m.Success)
  {
     return m.Groups[1];
  }
  else
  {
     return "Default";
  }
}

      



This returns a match (with the phrase you are looking for as a match) or no match (which means you should use Default).

+2


a source







All Articles