Home All Groups Group Topic Archive Search About

Convert Words in Strings to Caps

Author
22 Feb 2006 1:44 AM
Dennis
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

--
Dennis in Houston

Author
22 Feb 2006 7:03 AM
gene kelley
On Tue, 21 Feb 2006 17:44:02 -0800, Dennis
<Den***@discussions.microsoft.com> wrote:

Show quoteHide quote
>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
Author
23 Feb 2006 12:17 AM
Dennis
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


Show quoteHide quote
"gene kelley" wrote:

> On Tue, 21 Feb 2006 17:44:02 -0800, Dennis
> <Den***@discussions.microsoft.com> wrote:
>
> >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
>
Author
22 Feb 2006 7:04 PM
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

Show quoteHide quote
> 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
Author
23 Feb 2006 12:09 AM
Dennis
Thanks but don't think this works for other than space delimiters...I needed
a routine that would capitalize things like myname-hining, dennis.
--
Dennis in Houston


Show quoteHide quote
"Jim Wooley" wrote:

> 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
>
> > 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
>
>
>
Author
23 Feb 2006 3:29 PM
Jim Wooley
It does work for hyphen delimeted. I altered the sample on http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemGlobalizationTextInfoClassToTitleCaseTopic.asp
and got the following results:

testing-hYPhen to lowercase: testing-hyphen
testing-hYPhen to uppercase: TESTING-HYPHEN
testing-hYPhen to titlecase: Testing-Hyphen

It doesn't work with Mc/Mac/O' however.

Jim Wooley

Show quoteHide quote
> Thanks but don't think this works for other than space delimiters...I
> needed a routine that would capitalize things like myname-hining,
> dennis.
>
> "Jim Wooley" wrote:
>
>> 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