PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET Convert Words in Strings to Caps

Reply

Convert Words in Strings to Caps

 
Thread Tools Rate Thread
Old 22-02-2006, 01:44 AM   #1
=?Utf-8?B?RGVubmlz?=
Guest
 
Posts: n/a
Default Convert Words in Strings to Caps


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
  Reply With Quote
Old 22-02-2006, 07:03 AM   #2
gene kelley
Guest
 
Posts: n/a
Default Re: Convert Words in Strings to Caps

On Tue, 21 Feb 2006 17:44:02 -0800, Dennis
<Dennis@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
  Reply With Quote
Old 22-02-2006, 07:04 PM   #3
Jim Wooley
Guest
 
Posts: n/a
Default Re: Convert Words in Strings to Caps

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



  Reply With Quote
Old 23-02-2006, 12:09 AM   #4
=?Utf-8?B?RGVubmlz?=
Guest
 
Posts: n/a
Default Re: Convert Words in Strings to Caps

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


"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

>
>
>

  Reply With Quote
Old 23-02-2006, 12:17 AM   #5
=?Utf-8?B?RGVubmlz?=
Guest
 
Posts: n/a
Default Re: Convert Words in Strings to Caps

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


"gene kelley" wrote:

> On Tue, 21 Feb 2006 17:44:02 -0800, Dennis
> <Dennis@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
>

  Reply With Quote
Old 23-02-2006, 03:29 PM   #6
Jim Wooley
Guest
 
Posts: n/a
Default Re: Convert Words in Strings to Caps

It does work for hyphen delimeted. I altered the sample on http://msdn.microsoft.com/library/d...leCaseTopic.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

> 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 Woole



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off