PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Convert Words in Strings to Caps
Forums
Newsgroups
Microsoft DotNet
Microsoft VB .NET
Convert Words in Strings to Caps
![]() |
Convert Words in Strings to Caps |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 > > > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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 > |
|
|
|
#6 |
|
Guest
Posts: n/a
|
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 |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

