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.