Saturday, February 25, 2017

#1 2005-10-21 06:32:45 am

julifos
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

How can I find specific characters in a text? (eg, numbers, capitals)

This FAQ applies to someone looking for special characters in a text. For example: numbers, capitals, accented characters, etc.

There is not a built-in function in AppleScript, so you should use any regexp tool (scripting addition or "do shell script" call) or ellaborate your own method depending on your needs.

In this quick example, we will delete all found numbers in a text:

Applescript:

stripNumbers("I'm the number 1.") --> "I'm the number ."

to stripNumbers(inputText)
   set theNumbers to "0123456789"
   
   set prevTids to AppleScript's text item delimiters
   repeat with i in theNumbers
       set AppleScript's text item delimiters to i
       set inputText to inputText's text items
       set AppleScript's text item delimiters to prevTids
       set inputText to inputText as text
   end repeat
   
   inputText
end stripNumbers

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.038 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)