Re: Convert first char to lower case
Re: Convert first char to lower case
- Subject: Re: Convert first char to lower case
- From: Nigel Garvey <email@hidden>
- Date: Tue, 8 Jan 2002 02:28:25 +0000
Olivier Berquin wrote on Mon, 07 Jan 2002 15:20:03 +0100:
>
Hello everybody,
>
>
>
I wrote this handler, but I'm not sure that it's very powerfull.
>
It convert the first character of a var to lower case.
>
'ASCII number' and 'ASCII character' can be quite slow if you have to use
them a lot. In AppleScript, it's more convenient to look for the first
character in an alphabetical string of the upper-case characters, and to
replace it with the same-numbered character from a lower-case string.
If a 'return' occurs as the result of an 'if' line, there's actually no
need for an 'else' section, as what follows can only be executed if the
'return' isn't. (But there's nothing to stop you including 'else' for
clarity if you prefer.)
You don't need to count the 'characters' of a string. Just count the
string. The characters will be counted by default and it will happen
faster.
--
set myName to "This character"
-- bla bla bla
set myName to ConvertFirstCharToLowerCase(myName)
on ConvertFirstCharToLowerCase(Variable1)
if (class of Variable1 is string) and ((count Variable1) > 0) then
set upperChars to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set lowerChars to "abcdefghijklmnopqrstuvwxyz"
-- 'offset' is case-sensitive
set alphaPos to the offset of (character 1 of Variable1) in
upperChars
if (alphaPos > 0) then
if ((count Variable1) = 1) then return (character alphaPos of
lowerChars)
return (character alphaPos of lowerChars) & (text 2 thru -1 of
Variable1)
end if
end if
return Variable1
end ConvertFirstCharToLowerCase
NG