如何防止string.Replace()方法替换相似但不想要的数据?

时间:2019-06-22 23:13:06

标签: c# winforms replace

string.Replace()方法正在替换不应替换的字符。我正在尝试替换这样的日期:1/1 / 2019、2 / 2 / 2019、3 / 3/2019

日期如下:01/01/2019,02/02/2019,03/03/2019

问题是我的代码只为我提供:

2019年1月1日(如果我使用“ / 1 /”)或02/02/02019,具体取决于我使用的代码。

    //public readonly string[] dateStringErrors = { "/1/", "/2/", "/3/", "/4/", "/5/", "/6/", "/7/", "/8/", "/9/" }; // uncomment if below code fails
    public readonly string[] dateStringErrors = { "1/", "2/", "3/", "4/", "5/", "6/", "7/", "8/", "9/" };

    //public readonly string[] fixedDateStrings = { "/01/", "/02/", "/03/", "/04/", "/05/", "/06/", "/07/", "/08/", "/09/" }; // uncomment if below code fails
    public readonly string[] fixedDateStrings = { "01/", "02/", "03/", "04/", "05/", "06/", "07/", "08/", "09/" };

    public string FixDateStrings(string input)
    {
        //  Invalid Date Strings, 9 indexes in array
        for (int i = 0; i < 8; i++) // i represents index for dateStrings
        {
            input = input.Replace(dateStringErrors[i], fixedDateStrings[i]);
        }
        return input;
    }

注释版本的结果: (部分有效)

2019/10/18-$ 65.91

11/01/2019-$ 65.91

11/15/2019-$ 65.90


以上代码的结果: (完全休息)

2019/10/19-$ 65.91

101/02/2019-$ 65.91

101/106/2019-$ 65.90

问题很明显。我希望使日期均匀排列。


编辑: 我切换到monofont(Courier New),然后使用excel创建了所需的所有日期。

我的代码是意大利面条,它是2种形式,有大约4,000行代码,没有类/类库。我是一名新的C#开发人员,这是我的第一个实际项目。我需要学习课程库,但暂时我正在寻找“修补工作”

这是我最终想出的(日期被合并,复制,格式化,粘贴,并且一团糟,所以我必须这样做):

    public readonly string[] dateStringErrors = 
        { "1/1/", "1/2/", "1/3/", "1/4/", "1/5/", "1/6/", "1/7/", "1/8/", "1/9/", "1/10/", "1/11/", "1/12/",
        "1/13/", "1/14/", "1/15/", "1/16/", "1/17/", "1/18/", "1/19/", "1/20/", "1/21/", "1/22/", "1/23/", "1/24/", "1/25/", "1/26/", "1/27/", "1/28/",
        "1/29/", "1/30/", "1/31/", "2/1/", "2/2/", "2/3/", "2/4/", "2/5/", "2/6/", "2/7/", "2/8/", "2/9/", "2/10/", "2/11/", "2/12/", "2/13/", "2/14/",
        "2/15/", "2/16/", "2/17/", "2/18/", "2/19/", "2/20/", "2/21/", "2/22/", "2/23/", "2/24/", "2/25/", "2/26/", "2/27/", "2/28/", "2/29/", "3/1/",
        "3/2/", "3/3/", "3/4/", "3/5/", "3/6/", "3/7/", "3/8/", "3/9/", "3/10/", "3/11/", "3/12/", "3/13/", "3/14/", "3/15/", "3/16/", "3/17/", "3/18/",
        "3/19/", "3/20/", "3/21/", "3/22/", "3/23/", "3/24/", "3/25/", "3/26/", "3/27/", "3/28/", "3/29/", "3/30/", "3/31/", "4/1/", "4/2/", "4/3/",
        "4/4/", "4/5/", "4/6/", "4/7/", "4/8/", "4/9/", "4/10/", "4/11/", "4/12/", "4/13/", "4/14/", "4/15/", "4/16/", "4/17/", "4/18/", "4/19/",
        "4/20/", "4/21/", "4/22/", "4/23/", "4/24/", "4/25/", "4/26/", "4/27/", "4/28/", "4/29/", "4/30/", "5/1/", "5/2/", "5/3/", "5/4/", "5/5/",
        "5/6/", "5/7/", "5/8/", "5/9/", "5/10/", "5/11/", "5/12/", "5/13/", "5/14/", "5/15/", "5/16/", "5/17/", "5/18/", "5/19/", "5/20/", "5/21/",
        "5/22/", "5/23/", "5/24/", "5/25/", "5/26/", "5/27/", "5/28/", "5/29/", "5/30/", "5/31/", "6/1/", "6/2/", "6/3/", "6/4/", "6/5/", "6/6/",
        "6/7/", "6/8/", "6/9/", "6/10/", "6/11/", "6/12/", "6/13/", "6/14/", "6/15/", "6/16/", "6/17/", "6/18/", "6/19/", "6/20/", "6/21/", "6/22/",
        "6/23/", "6/24/", "6/25/", "6/26/", "6/27/", "6/28/", "6/29/", "6/30/", "7/1/", "7/2/", "7/3/", "7/4/", "7/5/", "7/6/", "7/7/", "7/8/", "7/9/",
        "7/10/", "7/11/", "7/12/", "7/13/", "7/14/", "7/15/", "7/16/", "7/17/", "7/18/", "7/19/", "7/20/", "7/21/", "7/22/", "7/23/", "7/24/", "7/25/",
        "7/26/", "7/27/", "7/28/", "7/29/", "7/30/", "7/31/", "8/1/", "8/2/", "8/3/", "8/4/", "8/5/", "8/6/", "8/7/", "8/8/", "8/9/", "8/10/", "8/11/",
        "8/12/", "8/13/", "8/14/", "8/15/", "8/16/", "8/17/", "8/18/", "8/19/", "8/20/", "8/21/", "8/22/", "8/23/", "8/24/", "8/25/", "8/26/", "8/27/",
        "8/28/", "8/29/", "8/30/", "8/31/", "9/1/", "9/2/", "9/3/", "9/4/", "9/5/", "9/6/", "9/7/", "9/8/", "9/9/", "9/10/", "9/11/", "9/12/", "9/13/",
        "9/14/", "9/15/", "9/16/", "9/17/", "9/18/", "9/19/", "9/20/", "9/21/", "9/22/", "9/23/", "9/24/", "9/25/", "9/26/", "9/27/", "9/28/", "9/29/",
        "9/30/", "10/1/", "10/2/", "10/3/", "10/4/", "10/5/", "10/6/", "10/7/", "10/8/", "10/9/", "11/1/", "11/2/", "11/3/", "11/4/", "11/5/", "11/6/",
        "11/7/", "11/8/", "11/9/", "12/1/", "12/2/", "12/3/", "12/4/", "12/5/", "12/6/", "12/7/", "12/8/", "12/9/" };

    public readonly string[] fixedDateStrings = 
        { "01/01/", "01/02/", "01/03/", "01/04/", "01/05/", "01/06/", "01/07/", "01/08/", "01/09/", "01/10/",
        "01/11/", "01/12/", "01/13/", "01/14/", "01/15/", "01/16/", "01/17/", "01/18/", "01/19/", "01/20/", "01/21/", "01/22/", "01/23/", "01/24/",
        "01/25/", "01/26/", "01/27/", "01/28/", "01/29/", "01/30/", "01/31/", "02/01/", "02/02/", "02/03/", "02/04/", "02/05/", "02/06/", "02/07/",
        "02/08/", "02/09/", "02/10/", "02/11/", "02/12/", "02/13/", "02/14/", "02/15/", "02/16/", "02/17/", "02/18/", "02/19/", "02/20/", "02/21/",
        "02/22/", "02/23/", "02/24/", "02/25/", "02/26/", "02/27/", "02/28/", "02/29/", "03/01/", "03/02/", "03/03/", "03/04/", "03/05/", "03/06/",
        "03/07/", "03/08/", "03/09/", "03/10/", "03/11/", "03/12/", "03/13/", "03/14/", "03/15/", "03/16/", "03/17/", "03/18/", "03/19/", "03/20/",
        "03/21/", "03/22/", "03/23/", "03/24/", "03/25/", "03/26/", "03/27/", "03/28/", "03/29/", "03/30/", "03/31/", "04/01/", "04/02/", "04/03/",
        "04/04/", "04/05/", "04/06/", "04/07/", "04/08/", "04/09/", "04/10/", "04/11/", "04/12/", "04/13/", "04/14/", "04/15/", "04/16/", "04/17/",
        "04/18/", "04/19/", "04/20/", "04/21/", "04/22/", "04/23/", "04/24/", "04/25/", "04/26/", "04/27/", "04/28/", "04/29/", "04/30/", "05/01/",
        "05/02/", "05/03/", "05/04/", "05/05/", "05/06/", "05/07/", "05/08/", "05/09/", "05/10/", "05/11/", "05/12/", "05/13/", "05/14/", "05/15/",
        "05/16/", "05/17/", "05/18/", "05/19/", "05/20/", "05/21/", "05/22/", "05/23/", "05/24/", "05/25/", "05/26/", "05/27/", "05/28/", "05/29/",
        "05/30/", "05/31/", "06/01/", "06/02/", "06/03/", "06/04/", "06/05/", "06/06/", "06/07/", "06/08/", "06/09/", "06/10/", "06/11/", "06/12/",
        "06/13/", "06/14/", "06/15/", "06/16/", "06/17/", "06/18/", "06/19/", "06/20/", "06/21/", "06/22/", "06/23/", "06/24/", "06/25/", "06/26/",
        "06/27/", "06/28/", "06/29/", "06/30/", "07/01/", "07/02/", "07/03/", "07/04/", "07/05/", "07/06/", "07/07/", "07/08/", "07/09/", "07/10/",
        "07/11/", "07/12/", "07/13/", "07/14/", "07/15/", "07/16/", "07/17/", "07/18/", "07/19/", "07/20/", "07/21/", "07/22/", "07/23/", "07/24/",
        "07/25/", "07/26/", "07/27/", "07/28/", "07/29/", "07/30/", "07/31/", "08/01/", "08/02/", "08/03/", "08/04/", "08/05/", "08/06/", "08/07/",
        "08/08/", "08/09/", "08/10/", "08/11/", "08/12/", "08/13/", "08/14/", "08/15/", "08/16/", "08/17/", "08/18/", "08/19/", "08/20/", "08/21/",
        "08/22/", "08/23/", "08/24/", "08/25/", "08/26/", "08/27/", "08/28/", "08/29/", "08/30/", "08/31/", "09/01/", "09/02/", "09/03/", "09/04/",
        "09/05/", "09/06/", "09/07/", "09/08/", "09/09/", "09/10/", "09/11/", "09/12/", "09/13/", "09/14/", "09/15/", "09/16/", "09/17/", "09/18/",
        "09/19/", "09/20/", "09/21/", "09/22/", "09/23/", "09/24/", "09/25/", "09/26/", "09/27/", "09/28/", "09/29/", "09/30/", "10/01/", "10/02/",
        "10/03/", "10/04/", "10/05/", "10/06/", "10/07/", "10/08/", "10/09/", "11/01/", "11/02/", "11/03/", "11/04/", "11/05/", "11/06/", "11/07/",
        "11/08/", "11/09/", "12/01/", "12/02/", "12/03/", "12/04/", "12/05/", "12/06/", "12/07/", "12/08/", "12/09/" };

{
        //  Re-Format Date Strings, 300 indexes in array
        for (int i = 0; i < 300; i++) // i represents index for dateStrings
        {
            input = input.Replace(dateStringErrors[i], fixedDateStrings[i]);
            input = input.Replace("101", "11"); // Fix November Date Formatting (also in a very inefficient way)
            input = input.Replace("102", "12"); // Fix December Date Formatting (also in a very inefficient way)
        }
        return input;
    }

不理想,但是可以。也许有一天可以帮助别人。

1 个答案:

答案 0 :(得分:9)

如果要处理日期,请为作业使用正确的工具/类型-DateTime

在特定情况下,如果您将日期作为字符串,则可以将其转换为DateTime,然后将其格式化为所需的字符串格式。

var givenDate = "1/31/2019";
var date = DateTime.ParseExact(
    givenDate, 
    @"M/d/yyyy", 
    System.Globalization.CultureInfo.InvariantCulture);

var output = date.ToString(@"MM/dd/yyyy"); // 01/31/2019