string fun

Hi…

if I have a string that reads:

“AppleBananaChocolateDonutEggplantFennelGrapes”

is there an easy way I can get applescript to chop it up by detecting capital letters, so it will end up making another string that reads:

“Apple, Banana, Chocolate, Donut, Eggplant, Fennel, Grapes”

?

The capital letters are between ASCII 65 and ASCII 90 so you can set up a repeat with k from 1 to count of characters in theString loop that gets the ASCII number of each character of the string and if it’s in the range, stores the value of the index in a list. Easy then to pick out the words from location Cap # 1 to (Cap # 2 - 1) etc. and build a list from the string.

An alternative way to detect certain letters is with an if character j of theString is (or is not, your choice) in (characters of “ABCDEFGHIJKLMN”) then, etc.

And as a retired Prof, I now say: “and the rest is left as an exercise for the reader”.

Another approach is Unix commands which always come in handy:

set cr to ASCII character (13)
set s to "AppleBananaChocolateDonutEggplantFennelGrapes"
set rslt to do shell script "echo " & s & "|sed 's/[A-Z][a-z]*/&\" & cr & "/g'"
set text item delimiters to cr
set foodL to text items of rslt
foodL -- {"Apple", "Banana", "Chocolate", "Donut", "Eggplant", "Fennel", "Grapes"}

-Dan

Duh, I should read more carefully what you’re asking for - you want a string, not a list:

set s to "AppleBananaChocolateDonutEggplantFennelGrapes"
set rslt to do shell script "echo " & s & "|sed 's/[A-Z][a-z]*/&, /g'"
set rslt to text 1 through -3 of rslt -- remove final ", "
rslt -- "Apple, Banana, Chocolate, Donut, Eggplant, Fennel, Grapes"

-Dan

Here’s a big, clunky AS method… :rolleyes:

set UpperCase to "ABCDEFGHIJKLMNOPQESTUVWXYZ"
set LowerCase to "abcdefghijklmnopqrstuvwxyz 1234567890"

set theString to "AppleBananaChocolateDonutEggplantFennelGrapes"
set theCharacters to characters of theString

set tmpWords to {}
set tmpWord to ""

repeat with tmpChar in theCharacters
	set tmpChar to (tmpChar as string)
	considering case
		if tmpChar is in UpperCase then
			if tmpWord is not "" then copy tmpWord to the end of tmpWords
			set tmpWord to tmpChar
		else if tmpChar is in LowerCase then
			set tmpWord to (tmpWord & tmpChar) as string
		end if
	end considering
end repeat

copy tmpWord to the end of tmpWords

set AppleScript's text item delimiters to {", "}
set tmpWords to (tmpWords as string)
set AppleScript's text item delimiters to {""}

return tmpWords
--> Returns: "Apple, Banana, Chocolate, Donut, Eggplant, Fennel, Grapes"

j

I can’t resist it. Here’s the same thing, slightly declunked… :wink:

set theString to "AppleBananaChocolateDonutEggplantFennelGrapes"

set _uppercase to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set tmpWords to {}

set i to 1
considering case
  repeat with j from 2 to (count theString)
    if character j of theString is in _uppercase then
      set end of tmpWords to text i thru (j - 1) of theString
      set i to j
    end if
  end repeat
  set end of tmpWords to text i thru j of theString
end considering

set AppleScript's text item delimiters to {", "}
set tmpWords to (tmpWords as string)
set AppleScript's text item delimiters to {""}

return tmpWords

With this particular input string, it’s about 11 times as fast as the shell script.