【问题标题】:Replace all 'S with 's unless there's a character after s?将所有 'S 替换为 ',除非 s 后面有一个字符?
【发布时间】:2019-09-01 14:05:42
【问题描述】:

我想创建转换的 ProperCase 文本函数:

O'Shanesy, Ben'S and frank'S tires

到:

O'Shanesy, Ben's and Frank's tires

如何让 strReturn.Replace("'S", "'s") 仅在 s 后没有字符时才起作用?

Public Function fProperCase(ByVal strTextIn As String, Optional ByVal bolIncludingAnd As Boolean = True) As String

        Dim strReturn As String = strTextIn.Trim

        strReturn = StrConv(strTextIn, VbStrConv.ProperCase)

        If bolIncludingAnd = False Then
            strReturn = strReturn.Replace("And", "and")
        End If

        'Change all 'S to 's unless there is 
        'another letter straight after
        strReturn.Replace("'S", "'s")

        Return strReturn

    End Function

【问题讨论】:

  • 应该这样做 - strReturn.Replace("'S ", "'s ")
  • AmarDuplantier 非常好,但如果短语是 O'Shanesy、Ben'S 和 frank'S 会怎样。更新:没问题我只能检查最后两个字符。谢谢。
  • 不幸的是,它不适用于标点符号,例如。如果是“O'Shanesy、Ben'S 和 frank's”,它仍然是“Ben'S”

标签: c# asp.net regex vb.net


【解决方案1】:

您可能需要使用正则表达式指示词尾。

\b - 一个分词,适用于空格和行尾。

strReturn = Regex.Replace(strReturn, "'S\b", "'s")

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2014-02-06
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2013-09-19
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多