Home > database >  String.Replace() for quotation marks
String.Replace() for quotation marks

Time:12-17

I am trying to run the following line of code to replace the Microsoft Word quotes with ones our database can store. I need to work around users copying strings from Microsoft Word into my textareas.

instrText = instrText.Replace("“", """).Replace("”", """)

I am getting syntax errors for the number of arguments.

enter image description here

I have tried character escapes and a couple other ways of formatting the arguments with no luck.

CodePudding user response:

This changes the 'smart' quotes from word,

    'non standard quotes
    Dim Squotes() As Char = {ChrW(8216), ChrW(8217)} 'single
    Dim Dquotes() As Char = {ChrW(8220), ChrW(8221)} 'double

    'build test string
    Dim s As String = ""
    For x As Integer = 0 To Squotes.Length - 1
        s &= x.ToString & Squotes(x) & ", "
    Next
    For x As Integer = 0 To Dquotes.Length - 1
        s &= (x   Squotes.Length).ToString & Dquotes(x) & ", "
    Next

    'replace
    For Each c As Char In Squotes
        s = s.Replace(c, "'"c)
    Next

    For Each c As Char In Dquotes
        s = s.Replace(c, ControlChars.Quote)
    Next

CodePudding user response:

Try the following:

Private Function CleanInput(input As String) As String
    DisplayUnicode(input)
    '8216 = &H2018  - left single-quote
    '8217 = &H2019  - right single-quote
    '8220 = &H201C  - left double-quote
    '8221 = &H201D  - right double-quote

    'Return input.Replace(ChrW(&H2018), Chr(39)).Replace(ChrW(&H2019), Chr(39)).Replace(ChrW(&H201C), Chr(34)).Replace(ChrW(&H201D), Chr(34))
    Return input.Replace(ChrW(8216), Chr(39)).Replace(ChrW(8217), Chr(39)).Replace(ChrW(8220), Chr(34)).Replace(ChrW(8221), Chr(34))
End Function

Private Sub DisplayUnicode(input As String)
    For i As Integer = 0 To input.Length - 1
        Dim lngUnicode As Long = AscW(input(i))

        If lngUnicode < 0 Then

            lngUnicode = 65536   lngUnicode
        End If

        Debug.WriteLine(String.Format("char: {0} Unicode: {1}", input(i).ToString(), lngUnicode.ToString()))
    Next

    Debug.WriteLine("")
End Sub

Usage:

 Dim cleaned As String = CleanInput(TextBoxInput.Text)

Resources:

Note: Also used Character Map in Windows.

CodePudding user response:

You have a solution that works above, but in keeping with your original form:

instrText = instrText.Replace(ChrW(8220), """"c).Replace(ChrW(8221), """"c)
  • Related