-
Feb 14th, 2009, 08:14 PM
#1
Thread Starter
Frenzied Member
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)
Last edited by noahssite; Feb 14th, 2009 at 08:26 PM.
-
Feb 15th, 2009, 04:40 AM
#2
Addicted Member
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)
-
Feb 15th, 2009, 10:35 AM
#3
Thread Starter
Frenzied Member
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.
-
Feb 15th, 2009, 01:16 PM
#4
Addicted Member
Re: Uppercase first letter in each word of a string.
Sry man
-
Feb 16th, 2009, 11:37 PM
#5
Re: Uppercase first letter in each word of a string.
I also have a post in CodeBank for the same topic here.
-
Oct 17th, 2011, 08:49 AM
#6
Member
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?
-
Oct 17th, 2011, 04:12 PM
#7
Thread Starter
Frenzied Member
Re: Uppercase first letter in each word of a string.
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)
-
Oct 17th, 2011, 10:14 PM
#8
Member
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
-
Oct 17th, 2011, 10:48 PM
#9
Thread Starter
Frenzied Member
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.
-
Oct 18th, 2011, 12:15 AM
#10
Member
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
Last edited by chathu1234; Oct 18th, 2011 at 12:16 AM.
Reason: Got the name wrong
-
Oct 18th, 2011, 12:20 AM
#11
Member
Re: Uppercase first letter in each word of a string.
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
-
Oct 20th, 2011, 08:16 AM
#12
Thread Starter
Frenzied Member
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.
Last edited by noahssite; Oct 20th, 2011 at 08:19 AM.
-
Mar 31st, 2013, 02:24 AM
#13
New Member
Re: Uppercase first letter in each word of a string.
Originally Posted by noahssite
vb Code:
MyString = Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(MyString)
thanks!
-
Aug 30th, 2019, 10:28 PM
#14
Registered User
Re: Uppercase first letter in each word of a string.
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|