Dillon Munien
Dillon Munien

Reputation: 15

Comparing Arrays while using substrings

Removing part of an array

So currently I have the following code

public static void ReadSuburbs()
{
    String directory = @"C:\Address Sorting\";
    string[] linesA = new string[5] 
    { 
        "41 Boundary Rd", 
        "93 Boswell Terrace", 
        "4/100 Lockrose St", 
        "32 Williams Ave", 
        "27 scribbly gum st sunnybank hills"
    };

    int found = 0;
    foreach (string s in linesA)
    {
        found = s.IndexOf("st");
        Console.WriteLine(s.Substring(found + 3));
    }
}

Currently I get the following result

Boundary Rd
Boswell Terrace
100 Lockrose St
Williams Ave
sunnybank hills

I was wondering if there was a way that I could, instead of removing characters, remove the first three words. For example if I have an array

string[] linesA = new string[5] 
{ 
    "41 Boundary Rd", 
    "93 Boswell Terrace", 
    "4/100 Lockrose St", 
    "32 Williams Ave", 
    "27 scribbly gum st sunnybank hills"
};

I want to remove every first three words in this array which will leave me with this as a result if i print to console.

st sunnybank hills

Upvotes: 0

Views: 57

Answers (2)

NoName
NoName

Reputation: 8025

Here is regular expression way:

string[] linesA = new string[5] { "41 Boundary Rd", "93 Boswell Terrace", "4/100 Lockrose St", "32 Williams Ave", "27 scribbly gum st sunnybank hills" };
Regex r = new Regex(@"^[^ ]* [^ ]* [^ ]* *");
foreach (string s in linesA)
{
    Console.WriteLine(r.Replace(s, ""));
}

[^ ] match any character except space.

Upvotes: 0

Tyress
Tyress

Reputation: 3653

Based on your example, what you want is to remove the first three words and not every 3rd word:

string[] linesA = new string[5] { "41 Boundary Rd", "93 Boswell Terrace", "4/100 Lockrose St", "32 Williams Ave", "27 scribbly gum st sunnybank hills"};
foreach (string line in linesA)
{
    string[] words = line.Split(' ');
    Console.WriteLine(string.Join(" ",words.Skip(3)));
}

Upvotes: 1

Related Questions