-1

Hi I have the folowing string:

* lalalalalaal
* 12121212121212
* 36363636363636
* 21454545454545454

every line of the list start with - "\r\n* "

is there a way to detect the "\r\n* " symbol at the beginning and maybe replace it with numbers 1, 2, 3, ...n. So in example something like this:

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

I imagine building an array and running the for loop would be required but i do not get my head around where I am supposed to start.

xyz16179
  • 23
  • 6

2 Answers2

0

You can use Linq and String.Replace to achieve it.(Believe you already have your strings as List as mentioned in second part of your OP)

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

In case, you do not have it as list, then you can split your string as

var result = str.Split(new string[]{Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)
                     .Select((x,index)=> $"{index+1}.{x.Replace("* ",string.Empty)}");
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • @HimBromBeere Thought it wasn't worth splitting it up ? your thoughts - would it really make it better if we split it up ? (so that i could also learn something) – Anu Viswan Dec 19 '18 at 16:05
  • @RufusL pls correct me if am wrong, but wasn't OP's requirement so ? He wanted to replace "\r\n* " with index ? My appologies if i understood it wrong – Anu Viswan Dec 19 '18 at 16:07
  • 1
    Possibly, I was going off his expected output and the title rather than the description, so you may be right. Just wanted to call it out. Also, what is `list`? If it's a `string` as the OP says, then the above won't compile (`char` does not have a `Replace` method). But again it's slightly unclear because he also calls it a list. – Rufus L Dec 19 '18 at 16:09
  • @RufusL honestly, OP wasn't quite clear. It mention the given input as string on the opening sentence, but the very next sentence it as called a list. So i assumed he already had a List, which was display as lame text in the sample – Anu Viswan Dec 19 '18 at 16:11
0

If I understand you correctly, you have a string that looks like this:

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

And you want to replace "\r\n*" with "\r\n1.", where the number 1 increments each time the search string is found.

If so, here's one way to do it: Use the IndexOf method to find the location of the string you're searching for, keep a counter variable that increments every time you find the search term, and then use Substring to get the sub strings before and after the part to replace ('*'), and then put the counter's value between them:

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

Here's a sample using your input string:

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

Output

enter image description here

Rufus L
  • 36,127
  • 5
  • 30
  • 43