Saturday, January 20, 2018

#1 2003-07-06 12:02:34 am

Trash Man
Sanitation Department
Registered: 2005-10-20
Posts: 5336

Change String Case with Diacritical Support

This script will change a string to either all lowercase or all uppercase and supports the option of removing diacriticals and replacing with plain text.

OS version: Any


property letters_uc : (characters of "ABCDEFGHIJKLMNOPQRSTUVWXYZÆŒØ?")
property letters_lc : (characters of "abcdefghijklmnopqrstuvwxyzæœø?")
property letters_dia_uc : (characters of "ÁÀÂÄÃÅÇÉÈÊËÍÌÎÏÑÓÒÔÖÕÚÙÛÜŸ")
property letters_dia_lc : (characters of "áàâäãåçéèêëíìîïñóòôöõúùûüÿ")
property letters_nondia_uc : (characters of "AAAAAACEEEEIIIINOOOOOUUUUY")
property letters_nondia_lc : (characters of "aaaaaaceeeeiiiinooooouuuuy")

set convert_case to "lower" --or "upper"
set strip_diacriticals to false --if true diacriticals will be removed and the plain text will be returned
return change_case(the_string, convert_case, strip_diacriticals)

on change_case(the_string, convert_case, strip_diacriticals)
   if strip_diacriticals = true then
       set letters_dia_uc_replace to letters_nondia_uc
       set letters_dia_lc_replace to letters_nondia_lc
       set letters_dia_uc_replace to letters_dia_uc
       set letters_dia_lc_replace to letters_dia_lc
   end if
   if convert_case = "lower" then
       set search_chars to letters_uc & letters_dia_uc
       set replace_chars to letters_lc & letters_dia_lc_replace
       set search_chars to letters_lc & letters_dia_lc
       set replace_chars to letters_uc & letters_dia_uc_replace
   end if
   set return_string to ""
   repeat with i from 1 to (count of characters of the_string)
       set string_char to (character i of the_string)
       if search_chars contains string_char then
           repeat with j from 1 to (count of search_chars)
               if (item j of search_chars) = string_char then
                   set return_string to return_string & (item j of replace_chars)
                   exit repeat
               end if
           end repeat
           set return_string to return_string & string_char
       end if
   end repeat
   return return_string as string
end change_case

One mans trash is another mans treasure



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)