Is there a Built in VB.net function to turn quotes into smartquotes
I want to turn
I'm addicted to 'peanuts' and "cocoa"
into
I’m addicted to 开发者_Python百科‘peanuts’ and “cocoa”
How would I do that? Any built in function for that?
Of course there is no builtin function. How on earth would you leave the 1st single quote unchanged and the 2nd changed. There has to be more logic behind it to give you the result you want. A program that interprets the english language construct, I guess
Of cource I noted the problem AFTER writing something, edit it for your needs, so it's not written for the cat's tail
Function trSmart(s As String) As String
Dim inSngl As Boolean = False
Dim inDbl As Boolean = False
For i = 1 To s.Length()
If s(i - 1) = "'"c Then
If inSngl Then
s = s.Substring(0, i - 1) & Chr(146) & s.Substring(i)
inSngl = False
Else
s = s.Substring(0, i - 1) & Chr(145) & s.Substring(i)
inSngl = True
End If
ElseIf s(i - 1) = """"c Then
If inDbl Then
s = s.Substring(0, i - 1) & Chr(147) & s.Substring(i)
inDbl = False
Else
s = s.Substring(0, i - 1) & Chr(148) & s.Substring(i)
inDbl = True
End If
End If
Next
Return s
End Function
This will automatically detect whether a white space is before or after the string and act appropiately.
Public Function SmartQuotes(ByVal someString As String) As String
someString = " " + someString + " "
Dim oldstring = someString
someString = smarten(someString, "'"c, "‘"c, "’"c)
someString = smarten(someString, """"c, Chr(147), Chr(148))
If oldstring = someString Then
Return Trim(someString)
Else
Return SmartQuotes(someString)
End If
End Function
Public Function smarten(ByVal someString As String, ByVal original As Char, ByVal left As Char, ByVal right As Char) As String
If someString.Contains(original) Then
Dim index = someString.IndexOf(original)
If leftQuoteorRightquote(someString, index) Then
someString = someString.Remove(index, 1).Insert(index, left)
Else
someString = someString.Remove(index, 1).Insert(index, right)
End If
End If
Return someString
End Function
Public Function leftQuoteorRightquote(ByVal somestring As String, ByVal index As Integer) As Boolean
If somestring.Substring(index - 1, 1) = " " And somestring.Substring(index + 1, 1) <> " " Then
Return True
Else
Return False
End If
End Function
精彩评论