How to get only the number in a string?.
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to get only the number in a string?.
Hello Livecoders,
I have a string consist of number and letters
"sample(123)"
I will just ask is it possible to get only the numbers on that string? only the 123.
Thank you.
-snop21
I have a string consist of number and letters
"sample(123)"
I will just ask is it possible to get only the numbers on that string? only the 123.
Thank you.
-snop21
Re: How to get only the number in a string?.
Hi Snop21,
you could check for the ASCII value 0-9 = 48-57!
Here a quick li'l function
...
answer numbers_from_string("sdsdsd12dddd44")
## -> 1244
...
Best
Klaus
you could check for the ASCII value 0-9 = 48-57!
Here a quick li'l function

Code: Select all
function numbers_from_string tString
put empty into tRetValue
repeat for each char tChar in tString
if chartonum(tChar) > 47 AND chartonum(tChar) < 58 then
put tChar after tRetValue
end if
end repeat
return tRetValue
end numbers_from_string
answer numbers_from_string("sdsdsd12dddd44")
## -> 1244
...
Best
Klaus
Re: How to get only the number in a string?.
klaus,
WOW... That's magic...!
)
Thank you..
-snop21
WOW... That's magic...!

Thank you..
-snop21
Re: How to get only the number in a string?.
No, Livecode!snop21 wrote:WOW... That's magic...!)

You could also use:
Code: Select all
...
if isnumber(tChar) then
put tChar after tRetValue
end if
...
Re: How to get only the number in a string?.
Klaus,
I wish I could be good as like you..
)
-snop21
I wish I could be good as like you..

-snop21
Re: How to get only the number in a string?.
Hi snop21,
practice, practice, practice!
Well, I started learning Metacard, the grandfather of Livecode, in 1999 the "hard way",
means without any previous programming experience and without internet access for any
kind of help! Only try & error and that was a #$%&§ good training!
Let's talk about this again in a couple of years!
Best
Klaus
practice, practice, practice!

Well, I started learning Metacard, the grandfather of Livecode, in 1999 the "hard way",
means without any previous programming experience and without internet access for any
kind of help! Only try & error and that was a #$%&§ good training!

Let's talk about this again in a couple of years!
Best
Klaus
Re: How to get only the number in a string?.
Good Day Klaus,
Yah..! You are exactly right.. we were so lucky today, we can ask help anytime we want because of the Internet Era today..
)
I have followup question.
Is it possible to get only the 1st 4 character?. quiet complicated..hehe gonna put sample.. hehe
the string is =====> "(12) Boy90"
I want to get the number of the 1st 4 charcater of the string... So I will be getting ony the 12.
Regards
-snop21
Yah..! You are exactly right.. we were so lucky today, we can ask help anytime we want because of the Internet Era today..

I have followup question.
Is it possible to get only the 1st 4 character?. quiet complicated..hehe gonna put sample.. hehe
the string is =====> "(12) Boy90"
I want to get the number of the 1st 4 charcater of the string... So I will be getting ony the 12.
Regards
-snop21
Re: How to get only the number in a string?.
Hi.
True. You have access to an astonishing help resource. That is a big problem.
This happens with many new users. The learning curve for LC is not zero. In fact, though relatively short, it requires much effort and frustration. It just seems simple, as if the ability to read will make the necessary skills just come. They will not.
Your queries have to be homework for you. You have seen how to distinguish numbers within a string. You simply must learn how to work text, using the tools LC provides. Unless you do, you will very soon ask questions to which you will not get ready answers. Your questions, though hardly beyond the helper community's ability to answer, will nonetheless go beyond the scope of what they will be willing to provide. At that time your learning will grind to a halt.
Search the dictionary as best you can. Read and work the tutorials and lessons. Skim the user guide. Do write back often. But write back with problems you are having, code that will not work, and results you cannot understand. There is unlimited help here for that sort of thing. I, for one, cannot wait.
Craig Newman
True. You have access to an astonishing help resource. That is a big problem.
This happens with many new users. The learning curve for LC is not zero. In fact, though relatively short, it requires much effort and frustration. It just seems simple, as if the ability to read will make the necessary skills just come. They will not.
Your queries have to be homework for you. You have seen how to distinguish numbers within a string. You simply must learn how to work text, using the tools LC provides. Unless you do, you will very soon ask questions to which you will not get ready answers. Your questions, though hardly beyond the helper community's ability to answer, will nonetheless go beyond the scope of what they will be willing to provide. At that time your learning will grind to a halt.
Search the dictionary as best you can. Read and work the tutorials and lessons. Skim the user guide. Do write back often. But write back with problems you are having, code that will not work, and results you cannot understand. There is unlimited help here for that sort of thing. I, for one, cannot wait.
Craig Newman
Re: How to get only the number in a string?.
Hi Snop,snop21 wrote: the string is =====> "(12) Boy90"
I will be getting ony the 12.
after reading LiveCode User guide.pdf,
chapter: 6.3 Regular Expressions,
you might find interesting this solution:
Code: Select all
put "(12) Boy90" into myString
get matchText( mystring, "^\((\d+)\)", myNumber)
answer mynumber
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: How to get only the number in a string?.
Good Day Guys,
dunbarx,
--- Thank you for reminding me that I have to work also with myself alone not just waiting for somebody to solve my problem..!
)
---> I got it solved. I attached the script..
)
Thierry,
Thanks.! But there's a another way to do it.. not that complicated.. hehe See attached file..
)
Regards
-snop21
dunbarx,
--- Thank you for reminding me that I have to work also with myself alone not just waiting for somebody to solve my problem..!

---> I got it solved. I attached the script..

Thierry,
Thanks.! But there's a another way to do it.. not that complicated.. hehe See attached file..

Regards
-snop21
- Attachments
-
- checkstringnumber.livecode.zip
- sample
- (1.06 KiB) Downloaded 193 times
Re: How to get only the number in a string?.
Come on, nothing complicated here.snop21 wrote: Thierry,
Thanks.! But there's a another way to do it.. not that complicated..

Just has to learn the basics of regex.
If you don't like it, well that's Ok, but then it's a different story

And Yes, there is always many ways to do things, as with most of languages.
Regards,
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: How to get only the number in a string?.
Thierry,
Hehe..
) I am newbie of this PL.. that's why most everything of livecode for me is complicated.. haha
) I think I am improving.
Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".
-Regards
-snop21
Hehe..


Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".
-Regards
-snop21
Re: How to get only the number in a string?.
poor chickensnop21 wrote: Hehe..)
Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".

you're not English, as they are used to say:
and in my country:there is more than one way to skin a cat
So,who is right: the chicken, the cat or the rabbit?more than one way to cook a rabbit
my 5 minutes crazyness an good luck with Livecode

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
Re: How to get only the number in a string?.
I am from the Philippines..
I am sorry if my english is not that good.. haha
Thanks Thierry!.. See you in my next post.. haha


Thanks Thierry!.. See you in my next post.. haha

Re: How to get only the number in a string?.
Oh, a nice place I missed to visit..snop21 wrote:I am from the Philippines..
Well, wasn't what I meant; I was intrigued only with the chickenI am sorry if my english is not that good..

and my English is limited too.
Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!