Remove white space in a string : Char Function « Data Type « VB.Net Tutorial






Public Class Tester
    Public Shared Sub Main
        Dim source As String = _
            Space(17) & "This    string    had " & Chr(12) & _
            StrDup(5, Chr(9)) & "extra whitespace. " & Space(27)
        Dim thisIsWhiteSpace As Boolean
        Dim prevIsWhiteSpace As Boolean
        Dim result As New System.Text.StringBuilder(source.Length)
        Dim counter As Integer

        For counter = 0 To source.Length - 1
            prevIsWhiteSpace = thisIsWhiteSpace
            thisIsWhiteSpace = Char.IsWhiteSpace(source.Chars(counter))
            If (thisIsWhiteSpace = False) Then
                If (prevIsWhiteSpace = True) AndAlso _
                   (result.Length > 0) Then result.Append(Space(1))
                result.Append(source.Chars(counter))
            End If
        Next counter
        Console.WriteLine(result.ToString())
    End Sub


End Class
This string had extra whitespace.








2.14.Char Function
2.14.1.Passing Char values to Val
2.14.2.Char: IsLetter, IsControl, IsDigit, IsLetterOrDigit, IsLower, IsNumber
2.14.3.Char: IsPunctuation, IsSeparator, IsSymbol, IsUpper, IsWhiteSpace
2.14.4.Char.IsDigit
2.14.5.Remove white space in a string
2.14.6.Char.ToUpper
2.14.7.ChrW
2.14.8.Demonstrates IsWhiteSpace.
2.14.9.Char.Equals.
2.14.10.Output ControlChars.Tab