2012-04-19 46 views
0

我有一個ProperCase功能在我的.Net代碼像這樣與hypens Propercase功能

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 

我需要propercase像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

任何想法?

+0

樣品輸入,輸出和預期產出將會有所幫助。我沒有看到你遇到的問題。 – Oded 2012-04-19 14:47:54

+0

@Oded我和預期產出更新 - 感謝 – 2012-04-19 15:01:46

回答

1

我不會改變分割方法都搭上連字符。相反,我會看看超弦。在改變它之前,從您的第一個方法導致。可能在循環內的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) 
+0

謝謝你正是我一直在後 – 2012-04-19 15:56:49