【问题标题】:String Replace()/regex replace - Replacing strings in array of strings?字符串替换()/正则表达式替换 - 替换字符串数组中的字符串?
【发布时间】:2012-04-26 00:55:05
【问题描述】:

我正在编写一个控制台应用程序,它从 csv 文件中读取数据并将文件中的每个元素存储到字符串数组中。有一种方法我想遍历数组中的每个字符串并删除所有非字母字符和空格。我使用 regex.replace() 成功地使用字符串执行此操作,但是一旦我尝试使用字符串数组执行此操作,情况就发生了变化。然后我继续尝试使用 string.replace() 但无济于事。我认为正则表达式路径是一个更好的选择,但我还没有成功。如果有人可以帮助我,我将不胜感激。到目前为止,这是我的代码:

    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);
    }

csv 文件包含用逗号分隔的不同地址。我的目标是删除数字,只留下街道名称。例如,原始字符串可能是 123 假的,目标是删除“123”,以便将其替换为“假”。我想对数组中的每个元素执行此操作。

【问题讨论】:

  • 你读过MSDN page for Regex.Replace吗? (A) 它返回 一个字符串,并且 (B) 不将整数作为第一个参数,因此它不会按原样编译。
  • 除非您的实际代码与此示例有些不同,否则您只能从地址数组中的文件中获取最后一个地址。您在 while 循环的每次迭代中都会覆盖它。

标签: c# regex arrays string replace


【解决方案1】:

您需要在替换时对结果做一些事情,类似以下的事情应该可以解决它。

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;
}

这里的关键是您必须将值传递到 RegEx.Replace 并更新您的数组。

【讨论】:

  • 哇。我有一种感觉,这很简单。非常感谢!
【解决方案2】:

除了米切尔的回答,这是一个问题:

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(',');
}

...并且可以替换为File.ReadAllLines:

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

您可以使用File.ReadLines 并即时修复地址:

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);
}

【讨论】:

  • 当您添加此内容时,我只是将其添加为评论。我在想 ReadAllLines,除非你需要另一个通过来分割行。使用List&lt;string[]&gt; 并保留流阅读器并将分割线添加到列表中可能会更好(也许)。
  • @pstrjds:你是对的,但 OP 已经通过了两次(读取并稍后调用 ChangeAddress
【解决方案3】:

为什么不将正则表达式替换应用于 strLine,然后再将其放入地址数组?您可以执行以下操作:

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

当然,您可能还想修改您的正则表达式以不删除 ','。

【讨论】:

    猜你喜欢
    • 2018-07-13
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多