除非用s后面没有字符,否则将所有'S替换为'?

时间:2019-04-11 02:53:10

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

我想创建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

1 个答案:

答案 0 :(得分:4)

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

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

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