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
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 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?
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
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
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
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.)
You must have gone wrong somewhere as your function doesn't capitalise the first letter.
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))
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
Even the spell-checker approach would fail given that more than one
capitalization happens to be valid, i.e."
de Vries
De vries
/Al
> >> "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.
This is great! Thanks guys! :-)
Yas
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