8 Replies Latest reply: Aug 13, 2006 12:28 AM by KOENIG Yvan
Bruce Kirman Level 2 Level 2 (170 points)
What would be the way to convert a lowercase text string to uppercase? Is there an uppercase function?

'bbbbb' to 'BBBBB'

Thanks in advance

iMac g5, Mac OS X (10.4.7)
  • Cohi Level 1 Level 1 (130 points)
    It would be easier to answer had you included the programming language you are using ... if it happens to be C++ you might try:

    #include <string>
    #include <ctype.h>

    void uppercase( std::string & s )
    {
    for ( std::string::size_type i = 0; i < s.size(); ++i )
    s[ i ] = ::toupper( s[ i ] );
    }


    Quad G5 / Mini G4 / PB15 G4    
  • dev_sleidy Level 4 Level 4 (1,570 points)
    set lowerCaseString to "abcdefghijklmnopqrstuvwxyz"

    set upperCaseString to ""
    repeat with i in lowerCaseString
    set upperCaseString to upperCaseString & (ASCII character ((ASCII number i) - 32))
    end repeat

    upperCaseString

      Mac OS X (10.4.4)  
  • Bruce Kirman Level 2 Level 2 (170 points)
    Thank You - I enhanced it a bit to ignore numbers and other characters - Works like a charm.....

    set lowerCaseString to "abd*&^&%^%ebewhjfvefe"

    set upperCaseString to ""
    repeat with i in lowerCaseString
    if (ASCII number i) > 96 and (ASCII number i) < 123 then
    set upperCaseString to upperCaseString & (ASCII character ((ASCII number i) - 32))
    else
    set upperCaseString to upperCaseString & (ASCII character (ASCII number i))
    end if
    end repeat

    upperCaseString
  • Cohi Level 1 Level 1 (130 points)
    (Sorry, my mistake -- I didn't see that this post was in the AppleScript forum.)
  • dev_sleidy Level 4 Level 4 (1,570 points)
    Re-written, to reduce the number of 'ASCII number i' calls.

    set lowerCaseString to "a1bd*&^&%^%e3bew5hjfv4efe"

    set upperCaseString to ""
    repeat with I in lowerCaseString
    set ascii_i to (ASCII number I)

    if ((ascii_i > 96) and (ascii_i < 123)) then
    set upperCaseString to upperCaseString & (ASCII character (ascii_i - 32))
    else
    set upperCaseString to upperCaseString & (ASCII character ascii_i)
    end if
    end repeat

    upperCaseString<br>
      Mac OS X (10.4.4)  
  • Simon Topliss1 Level 1 Level 1 (15 points)
    I have been using these set of handlers for a few years now. Much faster than calling Standard Additions.

    on lowercaseString(theString)
    set upperCaseChars to "ABCDEFGHIJKLMNOPQRSTUVWXYZÆŒØ∏ÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜŸ"
    set lowerCaseChars to "abcdefghijklmnopqrstuvwxyzæœøπáàâäãåçéèêëíìîïñóòôöõúùûüÿ"
    considering case
    repeat with i from 1 to theString's length
    set testChar to character i of theString
    if testChar is in upperCaseChars then
    set theString to getString(testChar, theString, upperCaseChars, lowerCaseChars)
    end if
    end repeat
    end considering
    return theString
    end lowercaseString

    on uppercaseString(theString)
    set upperCaseChars to "ABCDEFGHIJKLMNOPQRSTUVWXYZÆŒØ∏ÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜŸ"
    set lowerCaseChars to "abcdefghijklmnopqrstuvwxyzæœøπáàâäãåçéèêëíìîïñóòôöõúùûüÿ"
    considering case
    repeat with i from 1 to theString's length
    set testChar to character i of theString
    if testChar is in lowerCaseChars then
    set theString to getString(testChar, theString, lowerCaseChars, upperCaseChars)
    end if
    end repeat
    end considering
    return theString
    end uppercaseString

    on getString(testChar, theString, searchList, replaceList)
    set {oldDelims, AppleScript's text item delimiters} to ¬
    {AppleScript's text item delimiters, {testChar}}
    set charOffset to (length of (text item 1 of searchList)) + 1
    set textItems to text items of theString
    set theChar to (character charOffset of replaceList)
    set AppleScript's text item delimiters to {theChar}
    set theString to textItems as string
    set AppleScript's text item delimiters to oldDelims
    return theString
    end getString
  • Andreas Amann Level 5 Level 5 (7,815 points)
    Bruce,

    instead of using a pure AppleScript function which does character-by-character translation (which will be slow), you might want to consider a mixed AppleScript/shell script variant - the tr utility is pretty much what you are looking for...

    The following script has three handlers which will convert to uppercase, lowercase, or change the case of each letter:

    Click to open this script in your Script Editor<pre style="font-family: 'Monaco', 'Courier New', Courier, monospace; overflow:auto; padding: 0.2em; font-size: 10px; width:500px">set theString to "This is an example input string"

    log upperCase(theString)
    log lowerCase(theString)
    log changeCase(theString)

    on upperCase(theString)
    do shell script "echo " & quoted form of theString & " | tr '[a-z]' '[A-Z]'"
    end upperCase

    on lowerCase(theString)
    do shell script "echo " & quoted form of theString & " | tr '[A-Z]' '[a-z]'"
    end lowerCase

    on changeCase(theString)
    do shell script "echo " & quoted form of theString & " | tr '[A-Za-z]' '[a-zA-Z]'"
    end changeCase</pre>

    Andreas
  • Level 8 Level 8 (41,780 points)
    Hello Andreas

    Your script is efficient but only for those who never use accented chars.
    If the argument string contains accented chars as frequently used in euopean languages, they would not be converted.
    I know that this is frequent with shell tools but from my point of view, it's time to enhance these tools.

    Yvan KOENIG (from FRANCE dimanche 13 août 2006 09:29:00)