0

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.

madmike
  • 114
  • 1
  • 12

3 Answers3

0

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
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • I would like to keep it with the string.replace() if possible because I have other characters I am replacing with that statement. I removed them for the post to keep it simpler and they aren't the ones giving me issues. – madmike Dec 16 '21 at 17:16
  • What? Word has different start and end quote marks for both the single and double smart quotes so you'll need to do multiple replacements, so I don't know what your first statement means. – dbasnett Dec 16 '21 at 17:34
  • This is the full line I'm trying to run. Everything in there works except for the double quotes. That is why I am trying to keep it all in the same line of code if I can and not have a different statement to handle to double quotes. – madmike Dec 16 '21 at 17:55
  • instrText = instrText.Replace("–", "-").Replace("•", "-").Replace("…", ".").Replace("‘", "'").Replace("’", "'").Replace("“", """).Replace("”", """) – madmike Dec 16 '21 at 17:55
  • Sorry for the confusion. – madmike Dec 16 '21 at 17:56
  • I did get this to work just replacing the string values for the double quotes with the ChrW values in my string.replace() methods. Thanks. – madmike Dec 16 '21 at 19:12
0

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.

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
0

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

instrText = instrText.Replace(ChrW(8220), """"c).Replace(ChrW(8221), """"c)
djv
  • 15,168
  • 7
  • 48
  • 72