Remove Extra Whitespace In String


/ Published in: VB.NET
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ''' <summary>
  2. ''' Removes extra leading whitespace, trailing whitespace and inside whitespace
  3. ''' </summary>
  4. ''' <param name="_string">String to remove extra whitespace</param>
  5. ''' <returns>Cleaned string.</returns>
  6. ''' <remarks></remarks>
  7. Public Shared Function RemoveExtraWhitespace(ByVal _string As String) As String
  8. If (_string = Nothing Or _string = " ") Then
  9. Return Nothing
  10. End If
  11. _string = _string.Trim()
  12. Dim stringArray() As String = _string.Split(" ")
  13. Dim tempString As String = Nothing
  14. For Each stringElement In stringArray
  15. If (stringElement <> "") Then
  16. tempString += " " + stringElement
  17. End If
  18. Next
  19. tempString = tempString.Trim
  20. Return tempString
  21. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.