Question: If I have a string like **MCCORMIC 3H R Final 08-26-2011.dwg** or even **MCCORMIC SMITH 2N L Final 08-26-2011.dwg** and I wanted to capture the **R** in the first string or the **L** in the second string in a variable, what is the best method for doing so? I was thinking about trying the below statement but it does not work.

```
string filename = "MCCORMIC 3H R Final 08-26-2011.dwg"
string WhichArea = "";
int WhichIndex = 0;

WhichIndex = filename.IndexOf("Final");
WhichArea = filename.Substring(WhichIndex - 1,1); //Trying to get the R in front of word Final 

```

Answer: 
Just split by space:

```
var parts = filename.Split(new [] {' '}, 
                            StringSplitOptions.RemoveEmptyEntries);

WhichArea = parts[parts.Length - 3];

```

It looks like the file names have a very specific format, so this will work just fine.

Even with any number of spaces, using `StringSplitOptions.RemoveEmptyEntries` means spaces will not be part of the split result set.

Code updated to deal with both examples - [thanks Nikola](https://stackoverflow.com/questions/12287790/search-string-pattern/12287810#comment16483487_12287810).
I don't think [Oded's answer](https://stackoverflow.com/a/12287810/388664) covers all cases. The first example has two words before the wanted letter, and the second one has three words before it.

My opinion is that the best way to get this letter is by using RegEx, assuming that the word `Final` always comes after the letter itself, separated by any number of spaces.

Here's the RegEx code:

```
using System.Text.RegularExpressions;

private string GetLetter(string fileName)
{
    string pattern = "\S(?=\s*?Final)";
    Match match = Regex.Match(fileName, pattern);
    return match.Value;
}

```

And here's the explanation of RegEx pattern:

```
\S(?=\s*?Final)

\S // Anything other than whitespace
(?=\s*?Final) // Positive look-ahead
    \s*? // Whitespace, unlimited number of repetitions, as few as possible.
    Final // Exact text.

```