2

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 

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Oded- Sorry I should have been more specific. There can be undetermined spaces before the word Final
@DaBears - Answer updated (using the overload taking StringSplitOptions).
@Oded- I'm just trying to understand this. So this will just elminate the spaces? With an unlimited number of spaces possible before the word Final how does it know to get the letter R in the first string example?
This will not cover the case where the filename is MCCORMIC SMITH 2N L Final 08-26-2011.dwg.
2

I had to do something similar, but with Mirostation drawings instead of Autocad. I used regex in my case. Here's what I did, just in case you feel like making it more complex.

string filename = "MCCORMIC 3H R Final 08-26-2011.dwg"; string filename2 = "MCCORMIC SMITH 2N L Final 08-26-2011.dwg"; Console.WriteLine(TheMatch(filename)); Console.WriteLine(TheMatch(filename2)); public string TheMatch(string filename) { Regex reg = new Regex(@"[A-Za-z0-9]*\s*([A-Z])\s*Final .*\.dwg"); Match match = reg.Match(filename); if(match.Success) { return match.Groups[1].Value; } return String.Empty; } 

Comments

2

I don't think Oded's answer 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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.