Re: VBScript - String - first letters to Uppercase by Al
Al
Tue Sep 04 22:25:28 PDT 2007
"John W" <johnXZwilliams.XZesquire@XZgmail.com> wrote in message
news:-ZmdnZAQIPZTvEDbnZ2dneKdnZydnZ2d@pipex.net...
>
> "John W" <johnXZwilliams.XZesquire@XZgmail.com> wrote in message
> news:jKCdnQXEd7GrE0HbnZ2dnUVZ8v-dnZ2d@pipex.net...
>>
>> "Yas" <yasar1@gmail.com> wrote in message
>> news:1188828431.576261.18550@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"
>>> 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
>>>
>>
>> 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
>>
>
> 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))
> Next
>
> Wscript.Echo "Original: " & strName
> Wscript.Echo "Capitalised: " & Capitalise
>
> End Function
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