0

您好,我只是想知道是否有反转此代码以使第一个字母为小写而其余单词为大写?

Dim s As String = "the quick brown fox jumps over the lazy dog"
Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
MessageBox.Show(s2)

我正在使用 .net 框架 3.5

这是答案!谢谢蒂姆!

Dim s As String = TextBox1.Text
Dim s2 = String.Join(" ", s.Split() .Select(Function(w)UppercaseAllButFirst(w)).ToArray())
TextBox2.Text = s2
4

2 回答 2

1

你可以这样做:

dim s1 as string="the quick brown fox jumps over the lazy dog"
dim s2 as string= _ 
     string.Join(" ",s1.Split(" ").Select (function(s) _
     s.Substring(0,1).ToLower()+s.Substring(1).ToUpper()).ToArray())

这将为您提供以下输出:

tHE qUICK bROWN fOX jUMPS oVER tHE lAZY dOG
于 2012-10-30T09:40:08.190 回答
1

您可以创建一个新函数:

Public Shared Function UppercaseAllButFirst(s As String) As String
    ' check for empty string
    If (String.IsNullOrEmpty(s)) Then
        Return String.Empty
    End If
    Return Char.ToLower(s(0)) & s.Substring(1).ToUpper()
End Function

然后你可以这样使用它:

Dim s2 = String.Join(" ", s.Split().Select(Function(w) UppercaseAllButFirst(w)))

.NET 3.5,String.Join需要一个数组:

String.Join(" ", s.Split().Select(Function(w) UppercaseAllButFirst(w)).ToArray())
于 2012-10-30T09:38:28.017 回答