Q:

Removing a varying substring from strings in C#

I have a text file that lists addresses as follows.
123 Fake Street City State, Zip
124 Fake Street City State, Zip
...

I wrote a C# program that trims out the beginning numbers and filters out the streets (I am looking for unique streets in the data).  However, when there is an apartment in the address, they read like this.
123 Example Street Apt 12B City State, Zip
123 Example Street Apt 12C City State, Zip

With the way my code is written (since it looks for fully unique rows), it does not eliminate these apartment numbers.  What I want to do is remove the "apt" and ONLY the substring that follows it.  The problem is, these apartment numbers can be any length.  What would I put in the code below to remove apt + the apartment number?
if (line.ToLower.Contains(" apt "))
{
    //Remove apt + substring following it
}

A:

Since you know there will be a space after the apartment number, you can do this:
int aptStartIndex = line.ToLower().IndexOf(" apt ");
int aptEndIndex = line.IndexOf(" ", aptStartIndex + 5);
line = line.Substring(0, aptStartIndex) + line.Substring(aptEndIndex);

The first line finds the start of the "apt" string.
The second line finds the next space after " apt ".
The third line takes the substring up to " apt ", and adds the substring after the word after " apt ", effectively removing the two words.
I tested it HERE.

