Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. '***************************************************************************
  2. '*** Name   : CleanInvalidNameChars
  3. '*** Purpose: Clean up the input to chars that are not alpha, "'", ".", "-",
  4. '***          or " ".  Also removes consecutive spaces.
  5. '*** Inputs : dirtyString
  6. '*** Outputs:
  7. '***
  8. '*** Return : String
  9. '***************************************************************************
  10. Private Function CleanInvalidNameChars(ByVal dirtyString As String) As String
  11. Const ALPHAS = "[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]"
  12. Const PUNCS = "['.-]"
  13. Dim i As Long
  14. Dim strPartClean As String
  15. Dim strClean As String
  16. Dim ch As String
  17. Dim bFoundFirstAlpha As Boolean
  18. Dim arrNonConsecChars As Variant
  19.  
  20. For i = 1 To Len(dirtyString)
  21. ch = Mid(dirtyString, i, 1)
  22.  
  23. If ch Like ALPHAS Then
  24. strPartClean = strPartClean & ch
  25. bFoundFirstAlpha = True
  26. ElseIf bFoundFirstAlpha And (ch Like PUNCS Or ch = " ") Then
  27. strPartClean = strPartClean & ch
  28. End If
  29. Next
  30.  
  31. bFoundFirstAlpha = False
  32.  
  33. For i = Len(strPartClean) To 1 Step -1
  34. ch = Mid(strPartClean, i, 1)
  35.  
  36. If ch Like ALPHAS Then
  37. strClean = ch & strClean
  38. bFoundFirstAlpha = True
  39. ElseIf bFoundFirstAlpha And (ch Like PUNCS Or ch = " ") Then
  40. strClean = ch & strClean
  41. End If
  42. Next
  43.  
  44. arrNonConsecChars = Array(" ")
  45.  
  46. For i = LBound(arrNonConsecChars) To UBound(arrNonConsecChars)
  47. ch = arrNonConsecChars(i)
  48.  
  49. Do
  50. strClean = Replace(strClean, ch & ch, ch)
  51. Loop While InStr(1, strClean, ch & ch) > 0
  52. Next
  53.  
  54. CleanInvalidNameChars = strClean
  55. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement