Hi @JasonDaurison ,
You need to record the index of each space and insert them after sorting.
Here's an example you can refer to.
Private Function SortStringToLower(ByVal input As String) As String
Dim indexLst As List(Of Integer) = New List(Of Integer)
Dim pos As Integer = input.IndexOf(" "c)
While pos >= 0
indexLst.Add(pos)
pos = input.IndexOf(" "c, pos + 1)
End While
Dim characters As Char() = input.ToLower.Replace(" ", "").ToArray()
Array.Sort(characters)
Dim builder As StringBuilder = New StringBuilder(New String(characters))
For Each index In indexLst
builder.Insert(index, " "c)
Next
Return builder.ToString
End Function
Sub Main()
Dim str As String = "H ello Worl d"
Dim result As String = SortStringToLower(str)
Console.WriteLine(result)
Console.ReadLine()
End Sub
Result:
Best Regards,
Xingyu Zhao
*
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.