Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript - String - first letters to Uppercase

1,894 views
Skip to first unread message

Yas

unread,
Sep 3, 2007, 10:07:11 AM9/3/07
to
Hello,

Can anyone please help me with a function that takes a string, a name,
and makes all the first letters Uppercase?

Actually I can already kinda do the above but I'm stuck on initials eg
if I get...

"j.earl jones a.watson" I would like to make that "J.Earl Jones
A.Watson"
and of course if I get just "a.watson" turn that into "A.Watson"
or simply change "james" to "James" and "james watson" to James
Watson.

Basically all the 1st letters unless there is a "." in which case the
first letter also after the "."

It would also be nice to put some kind of a check in case I get
"j.earl." and there is nothing after the "." then leave it.

Thank you in advance :-)

Yas

McKirahan

unread,
Sep 3, 2007, 10:16:47 AM9/3/07
to
"Yas" <yas...@gmail.com> wrote in message
news:1188828431....@g4g2000hsf.googlegroups.com...

> Hello,
>
> Can anyone please help me with a function that takes a string, a name,
> and makes all the first letters Uppercase?

What about names where either the first letter should not be
capitalized and/or another letter should also be capitalized?

For example, "Diane von Furstenberg" and "DuPont".


mayayana

unread,
Sep 3, 2007, 10:20:30 AM9/3/07
to
> Actually I can already kinda do the above but I'm stuck on initials eg
> if I get...
>
> "j.earl jones a.watson" I would like to make that "J.Earl Jones
> A.Watson

That's really a syntax error. j.earl should be j. earl
It's the same situation as it would be at the end of
a sentence. How could you decide how to capitalize
in this: "the first sentence.the next sentence here."
...unless you just decide that all characters after "."
are in capitals. Wouldn't it be better to just require
proper syntax for the function to work?


mayayana

unread,
Sep 3, 2007, 12:47:45 PM9/3/07
to

> > Can anyone please help me with a function that takes a string, a name,
> > and makes all the first letters Uppercase?
>
> What about names where either the first letter should not be
> capitalized and/or another letter should also be capitalized?
>
> For example, "Diane von Furstenberg" and "DuPont".
>
That's an interesting point. I don't think it can be
accomodated short of a spell-checker type word list,
though. VB has an option of "proper case" with the
StrConv function, which capitalizes the first letter in
each word, but it won't recognize von in von Furstenburg.
(And unfortunately there's no StrConv in VBScript.)


Yas

unread,
Sep 3, 2007, 1:04:44 PM9/3/07
to

Hi All,

Thanks for the Input. I actually don't care too much about names that
don't start with capitals nor names where a centre letter should be
capitalised. As these display names are for my own record only so if
von is written as Von it's fine.

So still my requirements are listed above, make every first letter a
capital regardless of type of name make initial and first letter after
"." a capital eg j.jose -> J.Jose.

Many thanks

Yas

Yas

unread,
Sep 3, 2007, 4:31:26 PM9/3/07
to
On 3 Sep, 19:04, Yas <yas...@gmail.com> wrote:
> On 3 Sep, 18:47, "mayayana" <mayaXXyan...@mindXXspring.com> wrote:
>
> > > > Can anyone please help me with a function that takes a string, a name,
> > > > and makes all the first letters Uppercase?
>
> > > What about names where either the first letter should not be
> > > capitalized and/or another letter should also be capitalized?
>
> > > For example, "Diane von Furstenberg" and "DuPont".
>
> > That's an interesting point. I don't think it can be
> > accomodated short of a spell-checker type word list,
> > though. VB has an option of "proper case" with the
> > StrConv function, which capitalizes the first letter in
> > each word, but it won't recognize von in von Furstenburg.
> > (And unfortunately there's no StrConv in VBScript.)

Hi, I think my Function works now....here is what I have...I would be
greatful if anyone can make suggestions/improvments...
Thanks

Function ConvUCase(strName)

Dim name, int, myArray, elm

IF (InStr(strName," ")) Then

myArray = Split(strName, " ")
int = 0
For Each elm in myArray

myArray(int) = UCase(Left(myArray(int),1)) &
Right(myArray(int), Len(myArray(int))-1)

If (InStr(strName,".")) Then

myArray(int) = UCase(Left(myArray(int),InStr(myArray(int),".")+1))
& _
Right(myArray(int),(Len(myArray(int))) - (InStr(myArray(int),".")
+1))

End If
int = int + 1
Next

name = Join(myArray)

Else
If (InStr(strName,".")) Then

name = UCase(Left(strName,1)) & Right(strName,Len(strName)-1)
name = UCase(Left(strName,InStr(strName,".")+1)) & Right(strName,
(Len(strName)) - (InStr(strName,".")+1))
Else
name = UCase(Left(strName,1)) & Right(strName,Len(strName)-1)
End If
End IF

convUCase = name

End Function

Message has been deleted
Message has been deleted

John W

unread,
Sep 3, 2007, 6:43:59 PM9/3/07
to

"Yas" <yas...@gmail.com> wrote in message news:1188828431....@g4g2000hsf.googlegroups.com...

Your own function looks a bit complicated to me. Taking your 'spec' exactly as written and your later clarifications, try this:

Option Explicit

Capitalise ("j.earl jones a.watson")
Capitalise ("j. earl")
Capitalise ("j.earl.")
Capitalise ("a.watson")
Capitalise ("james")
Capitalise ("james watson")


Function Capitalise(strName)

Dim i, capNext

Capitalise = ""
capNext = True

For i = 1 To Len(strName)
If capNext Then
Capitalise = Capitalise & UCase(Mid(strName,i,1))
If Instr(". ",Mid(strName,i,1)) = 0 Then capNext = False
Else
Capitalise = Capitalise & Mid(strName,i,1)
If Instr(". ",Mid(strName,i,1)) > 0 Then capNext = True
End If
Next

Wscript.Echo "Original: " & strName
Wscript.Echo "Capitalised: " & Capitalise

End Function


mayayana

unread,
Sep 3, 2007, 10:32:37 PM9/3/07
to
Here's another approach that uses tokenizing. I don't
know whether this is the most efficient method, but
it's flexible if you want to make adaptations or add
complexity later.
For instance, if you decide that you also want all
letters after a colon or dash to be uppercase - or
not - those variations can be easily coded into
this function:

Function TokenizeProperCase(s1)
Dim i2, LenS1, BooNext, iAsc
Dim A1()
LenS1 = Len(s1)
ReDim A1(LenS1)
For i2 = 1 to LenS1
iAsc = Asc(Mid(s1, i2, 1))
Select Case iAsc
Case 46, 32, 9, 10 '-- ".", " ", vbLf, vbTab
BooNext = True
Case Else
If (BooNext = True) Then
If iAsc > 96 And iAsc < 123 Then iAsc = iAsc - 32
End If
BooNext = False
End Select
A1(i2) = Chr(iAsc)
Next
TokenizeProperCase = Join(A1, "")
End Function

Part of the reason I wrote it the way I did
was to avoid creating, concatenating, and releasing
a lot of little strings. I haven't tested this code on a large
input string to see how fast it is, but it seems like this
approach should be about as efficient as script
can get because while it's doing a lot of comparisons,
aside from loading the array it doesn't need to do any
other operations with variables in memory. It just
builds the new string in the array. (FAR more efficient
would be to have a byte or integer array that is then
converted directly to a string, but VBS can't do that
without doing a Chr() operation on each array
member.)

John W

unread,
Sep 4, 2007, 5:05:34 AM9/4/07
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message news:OLXTmwp7...@TK2MSFTNGP03.phx.gbl...

You must have gone wrong somewhere as your function doesn't capitalise the first letter.


John W

unread,
Sep 4, 2007, 5:13:45 AM9/4/07
to

"John W" <johnXZwillia...@XZgmail.com> wrote in message news:jKCdnQXEd7GrE0Hb...@pipex.net...

Taking the idea of allowing other characters to force the next character to be capitalised, here is my modified code. It also
takes advantage of the fact that 0 = false and >0 = true (the values returned by Instr), so is actually shorter and more efficient
than my original.

Option Explicit

Dim sNames, sName
sNames = Array("j.earl jones a.watson", "j. earl", "j.earl.", _
"a.watson", "james", "james watson")

For Each sName In sNames
Capitalise(sName)
Next


Function Capitalise(strName)

Dim i, capNext, capNextChars

Capitalise = ""
capNextChars = ". " & Chr(9) & Chr(10) '9=tab; 10=linefeed
capNext = True

For i = 1 To Len(strName)
If capNext Then
Capitalise = Capitalise & UCase(Mid(strName,i,1))

Else
Capitalise = Capitalise & Mid(strName,i,1)

End If
capNext = Instr(capNextChars,Mid(strName,i,1))

mayayana

unread,
Sep 4, 2007, 8:15:30 AM9/4/07
to

> You must have gone wrong somewhere as your function doesn't capitalise the
first letter.
>
Yes. That would need to be dealt with separately
if necessary. The function sets a flag for *the next
character* when it comes to a space, tab, etc. So
the very first character gets missed. I suppose the
easiest way to deal with that would be to just add
a space in front of the string.


Al Dunbar

unread,
Sep 5, 2007, 1:25:28 AM9/5/07
to

"John W" <johnXZwillia...@XZgmail.com> wrote in message
news:-ZmdnZAQIPZTvEDb...@pipex.net...

But if the end result is that only the initials are capitalized, shouldn't
perhaps all of the non-initial letters be de-capitalized? Otherwise the
assumption is that all input is lower case. For an example, make this change
to your last script:

> sNames = Array("J.EARL JONES A.WATSON")

/Al


Al Dunbar

unread,
Sep 5, 2007, 1:18:41 AM9/5/07
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message
news:uoCLupk7...@TK2MSFTNGP02.phx.gbl...

Even the spell-checker approach would fail given that more than one
capitalization happens to be valid, i.e."

de Vries
De vries

/Al


McKirahan

unread,
Sep 5, 2007, 2:53:19 AM9/5/07
to
"Al Dunbar" <Alan...@hotmail.com.nospaam> wrote in message
news:uqDW2937...@TK2MSFTNGP05.phx.gbl...
>

> >> "Yas" <yas...@gmail.com> wrote in message
> >> news:1188828431....@g4g2000hsf.googlegroups.com...
> >>> Hello,
> >>>
> >>> Can anyone please help me with a function that takes a string, a name,
> >>> and makes all the first letters Uppercase?
> >>>
> >>> Actually I can already kinda do the above but I'm stuck on initials eg
> >>> if I get...
> >>>
> >>> "j.earl jones a.watson" I would like to make that "J.Earl Jones
> >>> A.Watson"

> But if the end result is that only the initials are capitalized, shouldn't
> perhaps all of the non-initial letters be de-capitalized? Otherwise the
> assumption is that all input is lower case. For an example, make this
change
> to your last script:
>
> > sNames = Array("J.EARL JONES A.WATSON")

The original post (above) indicated that all letters start in lowercase.


Yas

unread,
Sep 5, 2007, 5:00:15 AM9/5/07
to
On 5 Sep, 08:53, "McKirahan" <N...@McKirahan.com> wrote:
> "Al Dunbar" <AlanD...@hotmail.com.nospaam> wrote in message

This is great! Thanks guys! :-)

Yas

Al Dunbar

unread,
Sep 17, 2007, 10:11:22 PM9/17/07
to

"McKirahan" <Ne...@McKirahan.com> wrote in message
news:2pCdnVQLOtAXzEPb...@comcast.com...

Interesting that we expect the user to forget to capitalize the initial
letters, but we take it for granted that is the only possible error. hAVEN'T
YOU EVER HIT THE CAPSLOCK KEY BY MISTAKE? i KNOW i HAVE.

/Al


0 new messages