How to move a word to the end of a string in VB.NET

1 Answer

0 votes
Imports System
Imports System.Linq
Imports System.Collections.Generic

Module Module1

    Function MoveWordToEnd(s As String, word As String) As String
        ' Split on whitespace
        Dim parts As List(Of String) =
            s.Split({" "}, StringSplitOptions.RemoveEmptyEntries).ToList()

        ' Remove the first occurrence
        If parts.Contains(word) Then
            parts.Remove(word)
            parts.Add(word)
        End If

        ' Rebuild the string
        Return String.Join(" ", parts)
    End Function

    Sub Main()
        Dim s As String = "Would you like to know more? (Explore and learn)"
        Dim word As String = "like"

        Dim result As String = MoveWordToEnd(s, word)
        Console.WriteLine(result)
    End Sub

End Module


' run:
'
' Would you to know more? (Explore and learn) like
'

 



answered Feb 5 by avibootz
...