Convert Words in Strings to Caps

G

Guest

I know this is probably a very overworked issue but thought I'd share the
code below to convert words in a text string to capitalize the first letter
of the word using an array of word delimiters. Hope it not too simplistic
for posting on thie newsgroup:

Private Overloads Function CapWords(ByVal textstring As String, ByRef
Delimiters() As Char) As String
If textstring Is Nothing OrElse textstring.Length <= 0 Then Return
Nothing
If textstring.length=1 then return textstring.ToUpper
Dim i As Integer = 1
Dim c() As Char = textstring.ToCharArray
c(0) = Char.ToUpper(c(0))
While i < c.Length - 1
If Array.IndexOf(Delimiters, c(i)) >= 0 Then c(i + 1) =
Char.ToUpper(c(i + 1))
i += 1
End While
Dim b As New StringBuilder
Return b.Append(c).ToString
End Function

Private Overloads Function CapWords(ByVal textstring As String, ByRefl
Delimiter As Char) As String
Dim Delimiters() As Char = {Delimiter}
Return CapWords(textstring, Caps, Delimiters)
End Function
 
G

gene kelley

I know this is probably a very overworked issue but thought I'd share the
code below to convert words in a text string to capitalize the first letter
of the word using an array of word delimiters. Hope it not too simplistic
for posting on thie newsgroup:

Private Overloads Function CapWords(ByVal textstring As String, ByRef
Delimiters() As Char) As String
If textstring Is Nothing OrElse textstring.Length <= 0 Then Return
Nothing
If textstring.length=1 then return textstring.ToUpper
Dim i As Integer = 1
Dim c() As Char = textstring.ToCharArray
c(0) = Char.ToUpper(c(0))
While i < c.Length - 1
If Array.IndexOf(Delimiters, c(i)) >= 0 Then c(i + 1) =
Char.ToUpper(c(i + 1))
i += 1
End While
Dim b As New StringBuilder
Return b.Append(c).ToString
End Function

Private Overloads Function CapWords(ByVal textstring As String, ByRefl
Delimiter As Char) As String
Dim Delimiters() As Char = {Delimiter}
Return CapWords(textstring, Caps, Delimiters)
End Function

I'm rewritting a VB6 app that has a routine to capitalize first letter
of strings of title and artist extracted from mp3 file tags. The tags
are often mixed case or can be all lower case. I have not rewritten
this routine yet, but your post on the subject caught my eye. My VB6
routine used a different approach. I curious how you handle some of
the special cases below.

How do you handle:
Roman Numerals - vii s/b VII, or ignored, but not Vii
Acronyms - cd s/b CD, and not Cd, but jr. s/b Jr.
Certain Names - o'conner s/b O'Conner, but not O'conner
word1/word2 - s/b Word1/Word2, but not Word1/word2

Thanks,

Gene
 
J

Jim Wooley

As an alternative, you can use the TextInfo.ToTitleCase method as follows:

Dim myTI As System.Globalization.TextInfo = New System.Globalization.CultureInfo("en-US",
False).TextInfo
Return myTI.ToTitleCase(MyString)

Jim Wooley
 
G

Guest

Thanks but don't think this works for other than space delimiters...I needed
a routine that would capitalize things like myname-hining, dennis.
 
G

Guest

How do you handle:
Roman Numerals - vii s/b VII, or ignored, but not Vii
Acronyms - cd s/b CD, and not Cd, but jr. s/b Jr.
Certain Names - o'conner s/b O'Conner, but not O'conner
word1/word2 - s/b Word1/Word2, but not Word1/word2

The first letter after a delimiter gets capitalized and that's all it does.
You can pass an array of delimiters ( ,;,-, etc). If you want special cases,
then add them to the routine using the .replace method for some but names
like o'conner will get capitalized to O'Conner if you pass a ' as one of the
delimiters but also don't would end up Don'T. The possibilities are endless
like McDonald, etc.

I also am using this for my Music LIbrarian program and it get's most of the
cases. I handle others manually that don't fit. I think you will find that
will be the best way since the possible exceptions are endless except for a
few common ones. --
Dennis in Houston
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top