How to sort the words in a string with VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic

Public Class SortTheWordsInAString_VB
    Public Shared Function SortTheWordsInAString(ByVal s As String) As String
        Dim arr As List(Of String) = New List(Of String)()
        Dim tokens As String() = s.Split(" "c)
        
		arr.AddRange(tokens)
        arr.Sort()
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()

        For Each str As String In arr
            sb.Append(str).Append(" ")
        Next

        If sb.Length > 0 Then
            sb.Length -= 1
        End If

        Return sb.ToString()
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim s As String = "php c java vb c++ python c#"
	
        s = SortTheWordsInAString(s)
	
        Console.WriteLine(s)
    End Sub
End Class




' run:
'
' c c# c++ java php python vb
'

 



answered Jul 29, 2024 by avibootz
...