none
String exercise RRS feed

  • Question

  • Points to all who provide a solution, kudos for a three liner, line 1 is Dim line, line 2 the operation, line 3 results

    For non admin and non MVP's

    Given 

    Dim line As String = " Visual Basic, ASP.NET  ,,    WPF"

    Expected result

    Visual Basic,ASP.NET,WPF
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 7, 2019 4:19 PM
    Moderator

Answers

All replies

  • Like this?

          Dim line As String = " Visual Basic, ASP.NET  ,,    WPF"
          Dim result As String = line.Replace(" ", "").Replace(",,", ",")
          Console.WriteLine(result)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Thursday, November 7, 2019 8:12 PM
  • We have one solution there are others, give it a try



    Thursday, November 7, 2019 8:24 PM
    Moderator
  • Like this?

          Dim line As String = " Visual Basic, ASP.NET  ,,    WPF"
          Dim result As String = line.Replace(" ", "").Replace(",,", ",")
          Console.WriteLine(result)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Yes this is fine.

    Since nobody else is responding do you want to try another solution as I mark all viable solutions as answered :-)


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 7, 2019 8:26 PM
    Moderator
  • Another approach:

          Dim line As String = " Visual Basic, ASP.NET  ,,    WPF"
          Dim result = Join((From s In line.Split(","c) Where Not String.IsNullOrEmpty(s) Select Trim(s)).ToArray, ",")
          Console.WriteLine(result)


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, November 8, 2019 3:14 AM