String.Replace() 用于引号
String.Replace() for quotation marks
我正在尝试 运行 下面的代码行将 Microsoft Word 引号替换为我们的数据库可以存储的引号。我需要解决用户将字符串从 Microsoft Word 复制到我的文本区域的问题。
instrText = instrText.Replace(""", """).替换(""", """)
我收到参数数量的语法错误。
我已经尝试过字符转义和其他几种格式化参数的方法,但没有成功。
这会更改单词中的 'smart' 引号,
'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
尝试以下操作:
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
用法:
Dim cleaned As String = CleanInput(TextBoxInput.Text)
资源:
- ASCII table
- C# How to replace Microsoft's Smart Quotes with straight quotation marks?
- How to represent Unicode character in VB.Net String literal?
注意: Windows.
中也使用了Character Map
您的解决方案在上面有效,但与您的原始形式保持一致:
instrText = instrText.Replace(ChrW(8220), """"c).Replace(ChrW(8221), """"c)
我正在尝试 运行 下面的代码行将 Microsoft Word 引号替换为我们的数据库可以存储的引号。我需要解决用户将字符串从 Microsoft Word 复制到我的文本区域的问题。
instrText = instrText.Replace(""", """).替换(""", """)
我收到参数数量的语法错误。
我已经尝试过字符转义和其他几种格式化参数的方法,但没有成功。
这会更改单词中的 'smart' 引号,
'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
尝试以下操作:
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
用法:
Dim cleaned As String = CleanInput(TextBoxInput.Text)
资源:
- ASCII table
- C# How to replace Microsoft's Smart Quotes with straight quotation marks?
- How to represent Unicode character in VB.Net String literal?
注意: Windows.
中也使用了Character Map
您的解决方案在上面有效,但与您的原始形式保持一致:
instrText = instrText.Replace(ChrW(8220), """"c).Replace(ChrW(8221), """"c)