0

ProperCase我的.Net 代码中有一个函数,如下所示

Public Function ProperCase(ByVal strValue As String) As String
    Dim outString As String = ""
    Dim badWords As String = "and, at, do, de, du, USA, UK"
    Dim splitter(1) As Char
    splitter(0) = " "
    Dim splitString As String() = strValue.Split(splitter)
    For Each s As String In splitString
        If badWords.Contains(s) Then
            outString = outString & s & " "
        Else
            outString = outString & StrConv(s, VbStrConv.ProperCase) & " "
        End If
    Next
    Return Trim(outString)
End Function

我需要对双桶名称进行适当的转换Taylor-Smith,但它的出现就像Taylor-smith因为拆分器是一个空格,所以我像这样修改了代码。

Public Function ProperCase(ByVal strValue As String) As String
    Dim outString As String = ""
    Dim badWords As String = "and, at, do, de, du, USA, UK"
    Dim splitter(2) As Char
    splitter(0) = " "
    splitter(1) = "-"
    Dim splitString As String() = strValue.Split(splitter)
    For Each s As String In splitString
        If badWords.Contains(s) Then
            outString = outString & s
        Else
            outString = outString & StrConv(s, VbStrConv.ProperCase)
        End If
    Next
    Return Trim(outString)
End Function

所以我在函数中添加了一个额外的拆分器,但现在它没有返回带有连字符的值。我& " "从 outString 的末尾删除了,但我不确定我可以用什么替换它。

我尝试添加& splitter,但即使分隔符是空格,它也总是返回连字符。

目前我正在用我修改过的代码得到这个

Mr TomHart Mr JamieTaylorSmith

有了第一个版本的代码,我得到了这个

Mr Tom Hart Mr Jamie Taylor-smith

我的预期输出是这样的......

Mr Tom Hart Mr Jamie Taylor-Smith

有任何想法吗?

4

1 回答 1

1

我根本不会改变 split 方法来捕捉连字符。相反,我会看看外线。由您更改之前的第一种方法引起的。可能在循环内部的 If 中。

这是一个非常快速的想法,它基于......不一定是最干净的版本,但应该给你这个想法:

    Dim outstring As String = "Michael James-smith"
    Dim indexOfCharToCheck As Integer = outstring.LastIndexOf("-"c) + 1
    Dim finalString As String = outstring.Substring(0, indexOfCharToCheck) & UCase(outstring(indexOfCharToCheck).ToString) & outstring.Substring(indexOfCharToCheck + 1)
    MsgBox(finalString)
于 2012-04-19T15:23:21.593 回答