Reverse Words in a Sentence using VB.NET
This example explains how to get words by using delimiter ' ' and then reverse each word using String>reverse.
String.Split(new char{' '});// Splits sentence into an array words
String.Reverse Reverses each word ,but returns IEnumerator<char>
need to convert it into an Array of char. then re-create String with
Array of characters.
Dim original As String = "But,in a larger sense,we can not dedicate - 1
not consecrate = not consecrate - 1
String()words=original.Split(New Char()
{
" "c
}
)
Dim i As Integer
For i = 0 To words.Length- 1 Step i + 1
Char()cWords = words(i).Reverse().ToArray()
words(i) = New String(cWords)
Next
Dim word As String
For Each word In words
Console.WriteLine(word)
Next
original=String.Join(" ", words)
Console.WriteLine(original)
End Sub
String.Join static function joins by array of words into a String.
Note: We used same delimiter for splitting and joining words.
OUTPUT
Input:But, in a larger sense, we can not dedicate-- we can
not consecrate-- we can not hallow-- this ground.
not consecrate-- we can not hallow-- this ground.
Output:,tuB ni a regral ,esnes ew nac ton --etacided ew ton
nac --etarcesnoc ew nac ton --wollah siht .dnuorg
nac --etarcesnoc ew nac ton --wollah siht .dnuorg
Tags: Reverse Words in a Sentence using VB.NET, Reverse word in VB.NET ,String.Reverse ,String.Join,Create new String using char array VB.NET.
No comments:
Post a Comment