Keep in mind that you wanted to do something to an array, so you probably needed an array method
This is a version of the search and replace that can optionally use regular expressions, as well as ignoring case and ignoring diacriticals.
on searchAndReplace:someText oldText:theOldText newText:theNewText usingRegex:regexFlag ignoringCase:caseFlag ignoringDiacrits:diacritsFlag -- ignore diacrits ignored for regex
-- build options
set theOptions to 0
if caseFlag then set theOptions to (current application's NSCaseInsensitiveSearch)
if regexFlag then
set theOptions to theOptions + (current application's NSRegularExpressionSearch as integer)
else -- ignoring diacriticals only applies if not doing regex
if diacritsFlag then set theOptions to theOptions + (current application's NSDiacriticInsensitiveSearch as integer)
end if
set theString to current application's NSString's stringWithString:someText
set theString to theString's stringByReplacingOccurrencesOfString:theOldText withString:theNewText options:theOptions range:{location:0, |length|:(theString's |length|())}
return theString as text
end searchAndReplace:oldText:newText:usingRegex:ignoringCase:ignoringDiacrits:
The split string one can't be done in ASObjC without a repeat, so it's a bit more complex.
on splitString:someText withString:mySeparator usingRegex:regexFlag ignoringCase:caseFlag ignoringDiacrits:diacritsFlag
-- build options
set theOptions to 0
if caseFlag then set theOptions to (current application's NSCaseInsensitiveSearch)
if regexFlag then
set theOptions to theOptions + (current application's NSRegularExpressionSearch as integer)
else -- ignoring diacriticals only applies if not doing regex
if diacritsFlag then set theOptions to theOptions + (current application's NSDiacriticInsensitiveSearch as integer)
end if
set theString to current application's NSString's stringWithString:someText
if theOptions = 0 then -- simplest case: literal, considering case and ingoring diacrits
set theList to theString's componentsSeparatedByString:mySeparator
else
set theList to {} -- to hold strings
-- get length and set some intital variables
set theFullLength to theString's |length|()
set theLength to theFullLength
set theStart to 0
repeat
-- look for range of next match
set theRange to theString's rangeOfString:mySeparator options:theOptions range:{location:theStart, |length|:theLength}
if theRange's |length|() = 0 then -- no match found, so grab the whole string then exit repeat
set end of theList to (theString's substringWithRange:{location:theStart, |length|:theLength}) as text
exit repeat
end if
-- grab the text before the match
set end of theList to (theString's substringWithRange:{location:theStart, |length|:(location of theRange) - theStart}) as text
-- reset values for next search
set theStart to current application's NSMaxRange(theRange) -- straight after the match
set theLength to theFullLength - theStart
end repeat
end if
return theList as list
end splitString:withString:usingRegex:ignoringCase:ignoringDiacrits: