是否有内置的VB.net功能将引号转换为智能引号

时间:2011-07-25 09:07:24

标签: vb.net built-in smart-quotes

我想转

我沉迷于'花生'和“可可”

进入

我沉迷于'花生'和“可可”

我该怎么做?任何内置函数?

2 个答案:

答案 0 :(得分:2)

当然没有内置功能。你究竟如何保持第一单引号不变,第二单引号改变。必须有更多的逻辑来为你提供你想要的结果。我想是一个解释英语语言结构的程序

对于cource我在写东西之后注意到了问题,根据你的需要编辑它,所以它不是为猫的尾巴写的

 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

答案 1 :(得分:0)

这将自动检测字符串之前或之后的空白区域是否适当。

  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