Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to get integer value from a string

I have some strings which contain number and other characters, such as "1 920 pixels". What I want is to get 1920 as integer.

Thanks in advance.

Posted on Jul 20, 2014 6:39 AM

Reply
30 replies

Jul 20, 2014 7:15 AM in response to MichaelRow12

As Frank Caggiano writes, what is your preferred programming language for this implementation?


Also and somewhat more subtly, what is the string encoding? If it's ASCII, then most available calls will work. If you're working with Unicode or UTF-8, then you'll probably want to use different routines to parse and retrieve the data.


strtok() and then atoi() in C are primitive, but would work. Using Objective C, NSString has methods to parse and retrieve data, and to return an integer value from a string. Python and most other languages also have conversions. Common finite state parsers such as Ragel, Lex/Yacc or Flex/Bison can usually return integer values, as well — using a parser can sometimes be easier than hand-coding the parsing, when dealing with big wads of text data.

Jul 21, 2014 2:30 AM in response to MichaelRow12

That's right because you have to extract the part that you want as a number as a substring first.


e.g.,


set s to "hello 1984"

set substringIndex to offset of "1984" in s

set substring to text substringIndex thru -1 of s as number



and going the other way:


set p to "1,920 pixels"

set substringIndex to offsetof "0" inp

set substring to text 1 thru (substringIndex) of p as number

Jul 21, 2014 2:31 AM in response to MrHoffman

That's my mistakes. Post this thread in a hurry and forget to describe in detail.

As for the text encoding, it may be UTF-8 by default In Apple Script. One of the methods is that: convert the encoding to ASCII and then compare each character with ASCII code, but it's a little inconvenient. I want to know a faster way to get number from string in Apple Script when "as integer" doesn't work (just like: "1 920 pixels" as integer)

Jul 21, 2014 2:58 AM in response to MichaelRow12

OK, here's a handler that should do it for you.


--use this line to specify the string and the number

returnNumber("the number 1,920 can be at the start, int middle or the end", 1920)



--place this handler at the beginning of your script

on returnNumber(myString, aNumber)

set aNumber to aNumber as text

set myString to myString as text

set substring to ""

set removeLeading to offsetof (text 1 of aNumber) inmyString

if removeLeading is greater than 1 then

set substring to text (removeLeading) thru -1 of myString

else

set substring to myString

end if

set removeTrailing to offsetof (text -1 of aNumber) insubstring

set substring to text 1 thru removeTrailing of substring as number


return substring


end returnNumber

Jul 21, 2014 3:23 AM in response to Phil Stokes

Ah, now there's a flaw in that handler, which is that repeated numerals will break it (e.g, "55" will return '5').


This modified handler should solve that problem, but note that you also must specify the number exactly as it is in the string and wrapped in quotes, ie "1,920" not 1920


returnNumber("there are 1,920 apples in my basket", "1,920")


on returnNumber(myString, aNumber)

set aNumber to aNumber as text

set numberCount to (count of aNumber)

set myString to myString as text

set substring to ""

set removeLeading to offsetof (text 1 of aNumber) inmyString

if removeLeading is greater than 1 then

set substring to text (removeLeading) thru -1 of myString

else

set substring to myString

end if

set substring to text 1 thru numberCount of substring as number


return substring


end returnNumber

Jul 21, 2014 4:09 AM in response to VikingOSX

I like this. 🙂 It 's more robust than messing around with text offsets.



I put it into an AppleScript for the OP:


set formula to "egrep -o \"(\\d{1,3},?\\d{1,3},?\\d{3}|\\d+)\" <<< \""


set s to quoted form of "your string with 1920 or 55 or any other number in it"


set term to "\""

do shell scriptformula & s & term



However, note one minor weakness which my revised handler above doesn't have: if the number is comma-separated like "1,920" it'll return


1

920

How to get integer value from a string

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.