TechGuy TechGuy - 2 months ago 6
ASP.NET (C#) Question

Build a string according to the no of delimiters

In here I want to build a string according to the no of delimiters.
In my scenario,
ListOfWords is ABC|EFG then builder is 1/2 but in myone it shows only 1

My c# Code is

var ListOfWords = "ABC|EFG";
var noOfOfSeperators = ListOfWords.Count(f => f == '|'); // in here it shows me 1
StringBuilder builder = new StringBuilder();
for (int i = 1; i <= noOfOfSeperators; i++) {
builder.Append(i).Append("/"); // i want to show the final result if this is 1/2
}

Answer
var ListOfWords = "ABC|EFG|HIJ";

var noOfOfSeperators = ListOfWords.Count(f => f == '|'); 

StringBuilder builder = new StringBuilder();
for (int i = 0; i <= noOfOfSeperators; i++)
{
    if(builder.Length>0)
    {
         builder.Append("/"); 
    }
    builder.Append(i+1);
}

var newString = builder.ToString();

OUTPUT:

1/2/3