Re: Convert first char to lower case
Re: Convert first char to lower case
- Subject: Re: Convert first char to lower case
- From: has <email@hidden>
- Date: Mon, 7 Jan 2002 23:46:53 +0000
Olivier Berquin wrote:
>
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.
>
>
Somebody can help me ???
Your script got a bit mashed by the list server (which trashes characters
whose ASCII number are greater than 127, e.g. "not equal to" and "is less
than or equal to" signs). Here's an unmashed version:
======================================================================
on ConvertFirstCharToLowerCase(Variable1)
if (class of Variable1 [NOT-EQUAL] string) or ((count of character
[NO-BREAK]of Variable1) = 0) then
return Variable1
else
if (ASCII number item 1 of Variable1) [GREATER-THAN-OR-EQUAL]
[NO-BREAK]65 and (ASCII number item 1 of Variable1)
[NO-BREAK][LESS-THAN-OR-EQUAL] 90 then
if (count of character of Variable1) = 1 then
set Variable1 to ASCII character ((ASCII number item 1 of
[NO-BREAK]Variable1) + 32)
else
set Variable1 to (ASCII character ((ASCII number item 1
[NO-BREAK]of Variable1) + 32)) & (characters 2 thru (count of
[NO-BREAK]character of Variable1) of Variable1)
end if
end if
return Variable1
end if
end ConvertFirstCharToLowerCase
======================================================================
[formatted using ScriptToEmail - gentle relief for mailing list pains]
[
http://files.macscripter.net/ScriptBuilders/ScriptTools/ScriptToEmail.hqx]
Two points I'd make:
1. "ASCII number" is an scripting addition call, so it takes a certain
amount of time for AS to send a message to it and get a response. You can
speed up your code by reducing the number of times this is used.
2. You don't need to say stuff like:
characters 2 thru (count of character of Variable1) of Variable1
Firstly, you can use negative numbers to indicate items at the end of a
string/list; e.g. -1 gets the last item, -2 gets the second last item, etc.
So you can simplify this to:
characters 2 thru -1 of Variable1
Secondly, when you get "characters x thru y", you actually get a list
containing each character: {"h", "i", "s",...} rather than a string.
Because you're concatenating this list onto the end of a string, it gets
coerced back to a string again and the result is the same; however, the
extra work involved means the code is slower. Instead, use the "text"
keyword, as in:
text 2 thru -1 of Variable1
-------
Here's the cleaned up version (which is also twice as fast). I've tweaked a
couple little bits for purely cosmetic reasons (i.e. so the list server
doesn't eat them), but you should be able to spot the significant changes
easily enough:
======================================================================
on ConvertFirstCharToLowerCase(Variable1)
if (class of Variable1 is not string) or (length of Variable1 = 0)
[NO-BREAK]then return Variable1
set Char1Num to ASCII number (character 1 of Variable1)
if Char1Num > 64 and Char1Num < 91 then
if (length of Variable1 = 1) then
return ASCII character (Char1Num + 32)
else
return (ASCII character (Char1Num + 32)) & (text 2 thru -1
[NO-BREAK]of Variable1)
end if
end if
end ConvertFirstCharToLowerCase
======================================================================
HTH
has