Usuwanie znaków specjalnych ze stringa

Musiałem tworzyć foldery, Windows nie akceptował znaków specjalnych. Więc musiałem te znaki usunąć z nazwy foldera.

Dodatkowo poniższa funkcja usuwa także polskie znaki. Wiedziałem, że nazwa będzie anglojęzyczna, więc mogłem z niej skorzystać.

Przykładowy kod VBA:

Dim MyString As String

Private Sub SomeString()
    MyString = "Ać(0ę%k8q"
    Debug.Print "It was: " & MyString
    AllClean (MyString)
    Debug.Print "It is : " & MyString

End Sub

' https://stackoverflow.com/questions/51624410/excel-vba-remove-all-special-characters-except-a-za-z0-9-from-a-string
Function AllClean(str As String) As String
    With CreateObject("vbscript.regexp")
        .Global = True
        .ignorecase = True
        .MultiLine = True
        .Pattern = "[^a-zA-Z0-9]"
        AllClean = .Replace(str, " ")
    End With
    MyString = AllClean
End Function

Output:

2 myśli w temacie “Usuwanie znaków specjalnych ze stringa

  1. Ta funkcja zostawia polskie znaki:

    Dim MyString As String

    Private Sub SomeString()
    MyString = "Ać(0ę %k8q."
    Debug.Print "It was: " & MyString
    AlphaNumeric (MyString)
    Debug.Print "It is : " & MyString

    End Sub

    'https://stackoverflow.com/questions/15723672/how-to-remove-all-non-alphanumeric-characters-from-a-string-except-period-and-sp
    Function AlphaNumeric(str As String) As String
    Dim i As Integer

    For i = 1 To Len(str)
    If InStr(1, "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzćę ", Mid(str, i, 1)) Then AlphaNumeric = AlphaNumeric & Mid(str, i, 1)
    Next
    MyString = AlphaNumeric
    End Function

  2. Nasal dilators work by pulling the nostrils and sides of the nose open, important like lifting the sides of a peaked roof to make further space on the inside. This can help to ease breathing through your nose and may reduce snoring.

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *

Witryna wykorzystuje Akismet, aby ograniczyć spam. Dowiedz się więcej jak przetwarzane są dane komentarzy.