【问题标题】:Detecting newline in string and adding a character before it检测字符串中的换行符并在其前添加一个字符
【发布时间】:2018-12-19 15:47:38
【问题描述】:

您好,我有以下字符串:

* lalalalalaal
* 12121212121212
* 36363636363636
* 21454545454545454

列表的每一行都以 - "\r\n* " 开头

有没有办法在开头检测"\r\n* " 符号,并可能将其替换为数字 1、2、3、...n。所以在示例中是这样的:

1. lalalalalaal
2. 12121212121212
3. 36363636363636
4. 21454545454545454

我想构建一个数组并运行 for 循环是必需的,但我不明白我应该从哪里开始。

【问题讨论】:

  • 你知道String.Replace
  • " 我有以下字符串:" 和“列表的每一行” 你有字符串还是列表?这是什么?
  • 字符串替换将是静态的,所以将是 1,1,1 我如何从这个字符串构建和数组运行一个循环,然后替换字符串?
  • 我会说这些行以* 开头并以\r\n 结尾....
  • 行不能以\r\n开头

标签: c# string list character


【解决方案1】:

如果我理解正确,您的字符串如下所示:

"\r\n* lalalalalaal\r\n* 12121212121212\r\n* 36363636363636\r\n* 21454545454545454"

并且您想将"\r\n*" 替换为"\r\n1.",其中1 的数字会在每次找到搜索字符串时递增。

如果是这样,这里有一种方法:使用IndexOf 方法查找您要搜索的字符串的位置,保留一个计数器变量,每次找到搜索词时递增,然后使用@ 987654328@获取替换部分前后的子字符串('*'),然后将计数器的值放在它们之间:

static string ReplaceWithIncrementingNumber(string input, string find, string partToReplace)
{
    if (input == null || find == null ||
        partToReplace == null || !find.Contains(partToReplace))
    {
        return input;
    }

    // Get the index of the first occurrence of our 'find' string
    var index = input.IndexOf(find);

    // Track the number of occurrences we've found, to use as a replacement string
    var counter = 1;

    while (index > -1)
    {
        // Get the leading string up to '*', add the counter, then add the trailing string
        input = input.Substring(0, index) +
                find.Replace(partToReplace, $"{counter++}.") +
                input.Substring(index + find.Length);

        // Find the next occurrence of our 'find' string
        index = input.IndexOf(find, index + find.Length);
    }

    return input;
}

这是使用您的输入字符串的示例:

static void Main()
{
    var input = "\r\n* lalalalalaal\r\n* 12121212121212\r\n* " + 
            "36363636363636\r\n* 21454545454545454";

    Console.WriteLine(ReplaceWithIncrementingNumber(input, "\r\n*", "*"));

    GetKeyFromUser("\nDone! Press any key to exit...");
}

输出

【讨论】:

    【解决方案2】:

    您可以使用 Linq 和 String.Replace 来实现它。(相信您已经将字符串作为 OP 的第二部分中提到的 List)

    var result = list.Select((x,index)=> $"{index+1}.{x.Replace("\r\n* ",string.Empty)}");
    

    如果你没有它作为列表,那么你可以将你的字符串拆分为

    var result = str.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)
                         .Select((x,index)=> $"{index+1}.{x.Replace("* ",string.Empty)}");
    

    【讨论】:

    • @HimBromBeere 认为不值得拆分吗?你的想法——如果我们把它分开真的会更好吗? (这样我也可以学到一些东西)
    • @RufusL 如果我错了,请纠正我,但不是 OP 的要求吗?他想用 index 替换 "\r\n* " 吗?如果我理解错了,我很抱歉
    • 可能,我偏离了他的预期输出和标题而不是描述,所以你可能是对的。就是想叫出来。另外,list 是什么?如果它是 OP 所说的 string,那么上面的代码将无法编译(char 没有 Replace 方法)。但同样有点不清楚,因为他也称其为列表。
    • @RufusL 老实说,OP 不是很清楚。它在开头的句子中将给定的输入作为字符串提到,但下一个句子称为列表。所以我假设他已经有一个 List,它在示例中显示为蹩脚的文本
    猜你喜欢
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多