Question

I've got a ProperCase function in my .Net code like so

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

I need to propercase double barrelled names like Taylor-Smith but it's coming out like Taylor-smith because the splitter is a space so I modified the code like so.

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

So I added an extra splitter into the function but now it's not returning the value with the hyphen in. I removed the & " " from the end of the outString but i'm not sure what I can replace it with.

I've tried to add & splitter but it always returns a hyphen even if the splitter was a space.

Currently I'm getting this with my modified code

Mr TomHart Mr JamieTaylorSmith

And With the first version of the code I got this

Mr Tom Hart Mr Jamie Taylor-smith

My expected outputs are like so...

Mr Tom Hart Mr Jamie Taylor-Smith

Any ideas?

Was it helpful?

Solution

I wouldn't alter the split method at all to catch the hyphens. Instead I would look at the outstring. Resulting from your first method before you changed it. Probably in the If inside the loop.

This is a really quick idea to base it on... not necessarily the cleanest version of it but should give you the idea:

    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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top