String Handling

Im used to having fairly powerful string handling functionality in the languages Ive been using.

How does Applescript stand up in comparison? Ive had a hard time finding any useful documentation from Apple. Is there a quickreference available somewhere?

What I want to do at the moment is split a long string up into lines, each line could quite happily be a list element. I havent even been able to get a simple loop over the string looking for new lines to work. How do you test for the value of a character?

if bob = “/” then
if bob is equal to “/” then
dont seem to work ( looping over a string with known /'s ). Im just popping up dialogue boxes when things happen to show the string value.

The most powerful tool available is the concept “text item delimiters”, and this is your lucky day, since it was published recently a great introductory article just here:
http://macscripter.net/unscripted/unscripted.php?id=32_0_1_0_C

About your current question, you already pointed the most usual method to compare strings:

bob = "/"
bob is equal to "/"

But in a repeat loop, the value assigned to a string, is not the string itself, but a reference to such string. Eg:

repeat with x in "hello"
     if x = "e" then beep
end repeat

Teorically, you should hear a “beep” in the second round, but you won’t, since the value of x will be:

x's first round --> item 1 of "hello"
x's second round --> item 2 of "hello"
...

So, you need anything such as:

repeat with x in "hello"
	if x as string = "e" then beep
end repeat

But this isn’t really reliable, since AppleScript, by default, ignores the case of the strings. So, you will beep here too:

repeat with x in "hello"
	if x as string = "E" then beep
end repeat

Then, you’d need this:

considering case
	repeat with x in "hello"
		if x as string = "E" then beep
	end repeat
end considering

Ffffff… Anyway, I allways prefer the following method (offset of), since it will work without additional problems:

repeat with x in "hello"
	if (offset of x in "e") is not equal to 0 then beep
end repeat

To break a string into paragraphs (no lines), you can use this:

set theString to "this is some sample
test and my mother is
not superman"

set stringParagraphs to paragraphs of theString
--> {"this is some sample", "test and my mother is", "not superman"}

The keyword “paragraph” will break a test using both CR and LF (asciis 13 & 10). If you need a different separator, you can use text item delimiters:

set theString to "this is a sample"

set AppleScript's text item delimiters to "i"
set theString to theString's text items
set AppleScript's text item delimiters to {""}
--> this is its default value, and for safety's sake, we allways restore it
theString --> {"th", "s ", "s a sample"}

More info, see the AppleScript Language Guide (you will find a quick link here: http://www.macscripter.net/ ). It’s a bit old, for most of it will work forever, specially text handling…