Derogation from the row of the sign



  • Got a list.

    List<string> lst = new List<string>(){"Привет",",","дорогой","друг","."};
    

    I'm moving the list to the line.

    var data = string.Join(" ", lst);
    

    As we should have expected, the exit line came

    Привет , дорогой друг .
    

    I don't like the commas too far from the text. Is there any way to use it? Join To the sign of retaliation?



  • List<string> lst = new List<string>(){"Привет",",","дорогой","друг","."};
    
    //массив символов
    //для исключения пробела между ними
    var excludes = new HashSet<string> {",","."};
    
    var result = string.Empty;
    
    foreach(var s in lst)
    {
        result += excludes.Contains(s) ? s : String.Concat(" ", s);
    }
    

    The result will be as follows: Hello, dear friend.

    example https://dotnetfiddle.net/FFwt6M


    As the commentaries have noticed, it is better to form this by extension, for example:

    public static class StringExtensions
    

    {
    public static string НазваниеМетода(this List<string>, HashSet<string> punctuations)
    {
    string result = string.Empty;
    foreach(var s in lst)
    {
    result += punctuations.Contains(s) ? s : String.Concat(" ",s);
    }
    return result;
    }
    }

    This will have to be applied:

    var result = lst.НазваниеМетода(excludes);

    The same result




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2