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.
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.
Program Language?
set x to "1920"
log class of x
set y to x as number
class of y
Replies:
(*text*)
Result:
integer
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.
egrep -o "(\d{1,3},?\d{1,3},?\d{3}|\d+)" <<< "See spot 1920 run 10,000 circles around Jane 999 who has fainted 1 time."
Output
1920
10,000
999
1
Forgot to mention. Sorroy for that.đź
I want to get Integer from string in Apple Script.
It works well when the a string has numbers only, but in other cases, it may crash.
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
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)
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
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
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
Phil,
Another day, another RE. The following will digest any optionally punctuated integer and/or decimal less than a trillion, and handle decimal fractions (e.g. .000456) too. I made certain it handles 1,920 correctly. đ
egrep -o "(\d+[.,]?\d+[.,]?\d+[.,]?\d+[.,]?\d?\d?|\d+[.,]?\d+|[.,]\d+)â
Cool, though you're not going to like the output of this version (Sorry!).
set formula to "egrep -o \"(\\d+[.,]?\\d+[.,]?\\d+[.,]?\\d+[.,]?\\d?\\d?|\\d+[.,]?\\d+|[.,]\\d+)\" <<< \""
set s to quoted form of "any number you like, (e.g., 1,920) but don't put a $ sign in front of it like $1,920 ;)"
set term to "\""
do shell scriptformula & s & term
You are soooo right. Fixed. Still wonât handle a space between currency symbol and number. Long enough as it is.
egrep -o "([$]?\d+[.,]?[$]?\d+[.,]?[$]?\d+[.,]?[$]?\d+[.,]?\d?\d?|[$]?\d+[.,]?[$]?\d+|[$ ]?[.,]\d+)"
egrep -o "([$]?\\d+[.,]?[$]?\\d+[.,]?[$]?\\d+[.,]?[$]?\\d+[.,]?\\d?\\d?|[$]?\\d+[.,]?[$] ?\\d+|[$]?[.,]\\d+)"
Long enough as it is.
Well, I'll mention that "\d" isn't portable. Of course replacing the "\d" with [[:digit:]] is portable...:-)
How to get integer value from a string