-1

I'm writing a console application that reads in from a csv file and stores each element from the file into an array of strings. There is one method which I want to iterate through each string in the array and remove all non alpha characters and spaces. I was successful doing this with a string using a regex.replace(), however that changed once I tried doing it with an array of strings. I then moved on to try using a string.replace() but to no avail. I think the regex path is a better option, but I havent been successful. If anyone could help me out I would greatly appreciate it. Here is my code so far:

    public static string[] ChangeAddress(string[] address)
    {
        for (int i = 0; i < address.Length; i++)
        {
            Regex.Replace(i, @"(\s-|[^A-Za-z])", ""); 
            System.Console.WriteLine(address[i]);
        }
        return address;
    }

    static void Main(string[] args)
    {
        string[] address = null;
        //try...catch read file, throws error if unable to read
        //reads file and stores values in array
        try
        {
            StreamReader sr = new StreamReader("test.csv");
            string strLine = "";
            //while not at the end of the file, add to array
            while (!sr.EndOfStream)
            {
                strLine = sr.ReadLine();
                address = strLine.Split(',');
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("File could no be read:");
            Console.WriteLine(e.Message);
        }

        //calls ChangeAddress method
        ChangeAddress(address);
    }

The csv file contains different addresses separated by commas. My goal is to remove the numbers and be left with just the street name. For instance the original string could be 123 fake, the goal is to remove the "123 " so it would be replace with just "fake". I want to do this to each element in the array.

2
  • Have you read the MSDN page for Regex.Replace? (A) it returns a string and (B) doesn't take an integer as the first argument so this won't compile as is. Commented Apr 13, 2012 at 16:26
  • Unless your actual code is somewhat different than this sample, you are only getting the last address out of the file in your address array. You are overwriting it in each iteration of your while loop. Commented Apr 13, 2012 at 16:31

3 Answers 3

2

You need to do something with the result when you replace, something similar the following should fix it.

public static string[] ChangeAddress(string[] address)
{
    for (int i = 0; i < address.Length; i++)
    {
        address[i] = Regex.Replace(address[i], @"(\s-|[^A-Za-z])", ""); 
        System.Console.WriteLine(address[i]);
    }
    return address;
}

The key here is that you have to pass the value into RegEx.Replace and also update your array.

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

1 Comment

Wow. I had a feeling it was something simple. Thank you very much!
1

In addition to Mitchel's answer, this is a problem:

StreamReader sr = new StreamReader("test.csv");
string strLine = "";

//while not at the end of the file, add to array
while (!sr.EndOfStream)
{
   strLine = sr.ReadLine();
   address = strLine.Split(',');
}

... and can be replaced with File.ReadAllLines:

addresses = File.ReadAllLines("test.csv");

You can use File.ReadLines and fix the address on the fly:

var addresses = new List<string>();
foreach(var address in File.Readlines("test.csv"))
{
    var corrected = Regex.Replace(address, @"(\s-|[^A-Za-z])", "");
    addresses.Add(corrected);
}

2 Comments

I was just adding that as a comment when you added this. I was thinking ReadAllLines, except then you need another pass to split the lines. It might be better (maybe) to use a List<string[]> and keep the stream reader and add the split lines to the list.
@pstrjds: You're right but OP is already taking two passes (reading in and later calling ChangeAddress)
0

Why not apply your Regular Expression replacement to strLine before you put it into your address array? You could just do something like the following:

`Regex.Replace(strLine, @"(\s-|[^A-Za-z])", "");`
`address = strLine.Split(',');`

Of course, you'd probably want to modify your Regex to not remove the ','s as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.