How to convert a string to Title Case

Joined
Mar 28, 2005
Messages
17
Programming Experience
Beginner
Hi Everyone,

I am retrieving a bunch of strings from a database. Some of them are in uppercase and some in lower case and some in mixed case.

My goal is to make all the strings Title Case. So like,

dim myString as string = "this is my String"

Is there something like myString.ToTitleCase just like we have myString.ToLower or myString.ToUpper?

So, after Title case, myString should look like "This Is My String"

Thanks,
Kevin
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
there isnt as of yet, but a function can be written for this though

VB.NET:
Friend Function TitleCase(ByVal Text As String) As String
	Dim strLastChar As Char
	Dim strCurrentChar As Char
	Dim strNewString As String
	Dim intCounter As Integer
	For intCounter = 1 To Text.Length
		strCurrentChar = Mid(Text, intCounter, 1)
		If strCurrentChar <> " " Then
			If strCurrentChar.IsLetter(strCurrentChar) = True Then
				If intCounter <> 1 Then
				    strLastChar = Mid(Text, intCounter - 1, 1)
				Else
				    strLastChar = " "
				End If
				If strLastChar = " " Then
				    strCurrentChar = strCurrentChar.ToUpper(strCurrentChar)
				End If
				strNewString &= strCurrentChar
			End If
		Else
			strNewString &= strCurrentChar
		End If
	Next intCounter
	Return strNewString
End Function

feel free to modify this for anything else it can be applied to
 
Last edited:

mathpath

Active member
Joined
Aug 21, 2004
Messages
27
Programming Experience
10+
I have a couple of comments to make, hopefully for the better.

1. Title case
The example given is for title case, which means that the less important words such as 'the', 'a', and so on will remain in lower case, and it appears that all words are in lower case except the first letter of important words.
A routine for this is a little harder to make, because the choice of words to remain in lower case is subjective. However, it is possible, if one is satisfied with a word list look-up. I do not know of any existing routine that does that, short of writing one.

2. The TitleCase routine by JuggaloBrotha
It appears to me that the routine will convert to, or maintain as the first letter of the line, and the first letter after a space, uppercase. It will not, however, convert any existing letter to lower case. So a title
"a tale of two CITIES" will be converted to "A Tale Of Two CITIES."
As I have not run the program on VB.net, I could err on that comment.

3. Yes, VB.NET will convert a line to Proper case, such as:

"a tale of two CITIES" will be converted to "A Tale Of Two Cities.", but it will not convert to "A Tale of two Cities".

You will find information on propercase under:
StrConv Function

ChrW Functions | String Data Type | Type Conversion Functions | ArgumentException




Returns a string converted as specified.

Public Shared Function StrConv( _
ByVal Str As String, _
ByVal Conversion As Microsoft.VisualBasic.VbStrConv, _
Optional ByVal LocaleID As Integer,
) As String

Where conversion can be:

VbStrConv.ProperCase Converts the first letter of every word in string to uppercase. Happy programming and hope this helps.
 
Top Bottom