views:

54

answers:

2

What's the most efficient way to pull culture information out of a resx's filename using C#? The solution should also handle there not being culture info in the file name (ie form1.resx). In that case a string assigned "Default" should be returned.

+2  A: 

Your best bet is just using 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 gets back a match (with the phrase you're looking for as the match), or no match (which means you should use "Default").

aronchick
Sorry, I asked the question poorly. I don't know exactly which culture will be there, I just need to grab it if it is between the periods.
Lee Warner
yep, my regex would work fine for that.
aronchick
+4  A: 

This seems like it would work:

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