Search code examples
c#stringstring-concatenation

Add a trailing # to each string of a list results in #//


I would like to add a trailing # to every string of a list of strings. The # is added, but as well two \. What can I do to correct this piece of code?

Input:

Houseplant

Algorithm:

tmpList = tmpList.Select(hashtag => Path.Combine("#", hashtag)).ToList();

Output:

#\\Houseplant

Solution

  • If you don't mean to concatenate these as paths (with a / separating them) then dont use Path.Combine.

    You could just use String.Concat:

    tmpList = tmpList.Select(hashtag => String.Concat("#", hashtag)).ToList();
    

    Live example: https://dotnetfiddle.net/Kjkxgu