2014-02-25 51 views

回答

3
Dim words = input.Split() 
Dim result = String.Format("{0} {1}", 
          words(0).ToUpper(), 
          String.Join(" ", words.Skip(1))) 

編輯:哦,只是第一個字,則使用words(0).ToUpper

+0

沒有驗證空字符串/字符串沒有空格? – MarcelDevG

+0

空字符串會引發異常。但是你是對的,它超出了問題的範圍。 – MarcelDevG

+0

@MarcelDevG:我根本不知道這段代碼的上下文。所以_maybe_它是一個方法,那麼處理一個空字符串可能是不正確的,在這種情況下'ArgumentNullException'會是適當的。但是,我只是我不知道它。 –

0
Public Function GetFirstWordUpperCase(ByVal input As String) As String 
    Return If(String.IsNullOrEmpty(input) Or String.IsNullOrWhiteSpace(input), Nothing, input.Split()(0).ToUpper()) 
End Function 

檢查是否輸入的字符串爲空,空或空白則返回你想要的東西,如果它不是。

例如,GetFirstWordUpperCase("how are you")回報 「HOW」

根本就MsgBox(GetFirstWordUpperCase("how are you"))顯示結果。

順便說一句,你可以拋出一個錯誤,並在你使用函數的地方捕獲它,這只是基本的想法。

相關問題