Uppercase first letter in each word of a string.
Here is very simple code to uppercase the first letter of each word in a string.
What i have done in this code is stored all the words into an array. Then by looping through the array the word is being split in two, the first part of the substring is the first letter. The first letter is then capitalized. After that is done the word is joined back together to make a whole. This process is done for every word in the array. As the process takes action the new edited word replaces the old word in the same array. The words from the array are being placed on a single line which will become the new value of Textbox1.
vb Code:
'Try something like so:
Dim words() As String
words = TextBox1.Text.Split(" "c)
For x As Integer = 0 To words.Rank + 1
words(x) = (words(x).Substring(0, 1).ToUpper & words(x).Substring(1, words(x).Length - 1))
Next
Dim NewString As String = String.Empty
For i As Integer = 0 To words.Rank + 1
If i = 0 Then
NewString = (words(i))
Else
NewString = (NewString & " " & words(i))
End If
Next
TextBox1.Text = NewString
EDIT: Here is an even simpilar code that will produce the same result as my code above.
vb Code:
MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)
Re: Uppercase first letter in each word of a string.
Good code but see this
vb Code:
Dim s As String = "the quick brown fox jumps over the lazy dog"
Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
MessageBox.Show(s2)
Re: Uppercase first letter in each word of a string.
Now that just makes my code look like a waste of CPU Usage...
The first reason i got it out of my signature.
Re: Uppercase first letter in each word of a string.
Re: Uppercase first letter in each word of a string.
I also have a post in CodeBank for the same topic here.
Re: Uppercase first letter in each word of a string.
Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?
Re: Uppercase first letter in each word of a string.
Quote:
Originally Posted by
chathu1234
Please can any one tell me how to do this while the user is entering data to the text box??? How can I modify this in text_changed function?
In the Textbox's keyup event place this code, assuming your textbox is TextBox1:
Code:
TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
Re: Uppercase first letter in each word of a string.
Thanks Noah but it will only allow to type backwards.example: if you type "upper case" it will give you "Esa Creppu"
How can I fix this
Re: Uppercase first letter in each word of a string.
You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.
e.g.
Code:
TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
TextBox1.SelectionStart = TextBox1.Length - 1
What is happening is that when the Textbox's value is changed the Selection of the Textbox is being reset to 0.
Re: Uppercase first letter in each word of a string.
Thank you Noah for taking your time to help me.But your code did not resolve the problem.Now when I enter the first letter the cursor jumps back to the beginning of the text.from then on in it works fine.
Ex: Input - upper case
Output - Pper Caseu
Can you help me please?
Im using VB 2008 and vb2010.Both do not support textbox.length
Had to use textbox.TextLength
Thank you so much
Re: Uppercase first letter in each word of a string.
Quote:
Originally Posted by
noahssite
You have to set the Selection of the Textbox to the length of the Textbox's value minus 1.
e.g.
Code:
TextBox1.Text = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(TextBox1.Text)
TextBox1.SelectionStart = TextBox1.Length - 1
What is happening is that when the Textbox's value is changed the Selection of the Textbox is being reset to 0.
I figured it out Noah.
Instead of
Code:
TextBox1.SelectionStart = TextBox1.Length - 1
I used
Code:
TextBox1.SelectionStart = TextBox1.TextLength + 1
It solved the issue.Thank you so much
Re: Uppercase first letter in each word of a string.
Oh my mistake.
This should have worked, I believe:
Code:
TextBox1.SelectionStart = TextBox1.Length
with no adding/subtracting.
Re: Uppercase first letter in each word of a string.
Quote:
Originally Posted by
noahssite
vb Code:
MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)
thanks!
Re: Uppercase first letter in each word of a string.
Quote:
Originally Posted by
stru4nak
Good code but see this
vb Code:
Dim s As String = "the quick brown fox jumps over the lazy dog"
Dim s2 As String = StrConv(s, VbStrConv.ProperCase)
MessageBox.Show(s2)
Thank you very much sir