Working with CFString (split string into array)


I have a multiline string where i want to extract a number.
The string looks like this:

Now I wanted to seperate the String into an array.
I found the following function:

I tried the following code:

set ausgabe_array to CFStringCreateArrayBySeparatingStrings_(null, ausgabe_string, ":")

I dont however get an array as output.

Does anybody have an example how to use this (or a similiar CFString) function?

Thanks in advance


CFStringCreateArrayBySeparatingStrings is a CoreFoundation function.
Better use the Cocoa method componentsSeparatedByString: of class NSString.

But for this purpose I’d prefer NSScanner

The problem with NSScanner, though, is that many of its methods use pointers to pointers (NSString **), so they can’t be used directly in ASObjC.

thanks, i tried it with componentsSeparatedByString like this:

set ausgabe to "one:two:three:four"
set ausgabe_array to ausgabe's componentsSeparatedByString_(":")
debug_("ergebnis: " & ausgabe_array)

“debug_” is just a function that puts a string inside a textfield to debug.

i also tried a few other things like
set ausgabe_array to componentsSeparatedByString_(ausgabe, “:”)
set ausgabe_array to componentsSeparatedByString_(ausgabe, “:”) as string
set ausgabe_array to ausgabe’s componentsSeparatedByString_(“:”) as string

none of it worked though.
Any idea how this would work?

You can’t call an NSString’s method on an AS string – you need to convert it to an NSString first. Something like:

set ausgabe to "one:two:three:four"
set ausgabe to current application's NSString's stringWithString_("one:two:three:four")
set ausgabe_array to ausgabe's componentsSeparatedByString_(":")

Thanks, it did work with the test string (one:two:three…).
I wanted to separate the string at each line, so the first line is the first index, the second line is the second index, etc.

Normaly (in PHP that is for me), i’d just use a “\n” as separator. This however doens’t work in here, as the \n is translated into an new line directly in the code.

Hava look at

Apple developer site Paragraphs and Line Breaks

Even though the “\n” adds a new line in the code, it will work. Sometimes you need to use “\r”
Another option is to use the AppleScript keyword return.

there is also a componentsSeparatedByCharactersInSet: method of class NSString,
use it with the predefined newlineCharacterSet. It contains \r, \n and unicode U+0085.
The equivalent ObjC code is

componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]

thanks mark.
I read the article and think the “NSParagraphSeparatorCharacter” is what I’m looking for.

I tried using it as a constant like this:

set ausgabearray to ausgabe's componentsSeparatedByString_(NSParagraphSeparatorCharacter)

Didn’t work.

I also made a few other tries like

set separator to current application's NSText's NSParagraphSeparatorCharacter
set separator to current application's NSText's NSParagraphSeparatorCharacter()

none of it worked though.
(sorry for being such a noob)

I don’t think so – that’s a Unicode separator apart from return and linefeed. I think Stefan was on the right track:

componentsSeparatedByCharactersInSet_(current application’s NSCharacterSet’s newlineCharacterSet())

I tried something simliart, thou i used “NSText’s NSParagraphSeparatorCharacter” instead of NSCharacterSet’s

Thanks a lot, it worked :slight_smile:
Now I just need to cut a string a little bit
I’ll find this out by myself

you guys are the best, i mean it :slight_smile: