T3rr11
T3rr11

Reputation: 57

Reversed First Letter Caps?

Hello i was just wondering is there anyway to reverse this code so that the first letter is lowercase and the rest of the word is caps?

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

I'm using .net frameworks 3.5

Here is the answer! Thanks Tim!

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

Upvotes: 0

Views: 170

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460158

You can create a new function:

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

Then you can use it this way:

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

.NET 3.5, String.Join needs an array:

String.Join(" ", s.Split().Select(Function(w) UppercaseAllButFirst(w)).ToArray())

Upvotes: 1

Arion
Arion

Reputation: 31239

You could do it like this:

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())

This will get you this output:

tHE qUICK bROWN fOX jUMPS oVER tHE lAZY dOG

Upvotes: 1

Related Questions