Jump to content

Cap only first letter

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Chan

Chan

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 204 posts
Is there a function or a way to only make the first letter of a word cap?

Like:



CODE or code (becomes) Code;




#2
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I would say whatever string you have, split it into two strings, the first letter being one string, and the rest of the word being the second string, then use the toupper and tolower functions on each of them to get the desired case, then put the string back together in one string.

string myString
string 1 = myString.firstletter
string 2 = myString.therest
1.toupper
2.tolower
myString = 1 + 2

#3
Void

Void

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 411 posts
Not too hard and basically just what hooser said.


  string upper = textBox1.Text.Substring(0,1).ToUpper();

  string lower = textBox1.Text.Substring(1).ToLower();


  string complete = String.Concat(upper, lower);


Replace textbox.text with your own string.
Void

#4
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
Or, you could use the (ghast!) Microsoft.VisualBasic.Strings.StrConv function with VbStrConv.ProperCase. That'll capitalize every word in your string.

As Billy Hollis says, it was very nice of the VB community to let us use their new framework from other languages.

#5
hoser2001

hoser2001

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I didnt know one could use VB functions in C# code... Thanks for the post brackett

#6
Ronin

Ronin

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
Hey, that is pretty cool. I didn't even know VB had a function that did this to start with. haha, I guess you have to know all the functions in .NET - but how do you go about learning them???