How to get only the number in a string?.

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

How to get only the number in a string?.

Post by snop21 » Sat Feb 22, 2014 5:08 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to get only the number in a string?.

Post by Klaus » Sat Feb 22, 2014 5:17 pm

Hi Snop21,

you could check for the ASCII value 0-9 = 48-57!

Here a quick li'l function :D

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

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Sat Feb 22, 2014 5:26 pm

klaus,


WOW... That's magic...! :))


Thank you..

-snop21

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to get only the number in a string?.

Post by Klaus » Sat Feb 22, 2014 5:36 pm

snop21 wrote:WOW... That's magic...! :))
No, Livecode! :D

You could also use:

Code: Select all

...
if isnumber(tChar) then
  put tChar after tRetValue
end if 
...

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Sat Feb 22, 2014 5:56 pm

Klaus,

I wish I could be good as like you.. :))


-snop21

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: How to get only the number in a string?.

Post by Klaus » Sat Feb 22, 2014 6:05 pm

Hi snop21,

practice, practice, practice! 8)

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! :D

Let's talk about this again in a couple of years!


Best

Klaus

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Mon Feb 24, 2014 4:39 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: How to get only the number in a string?.

Post by dunbarx » Mon Feb 24, 2014 5:28 am

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to get only the number in a string?.

Post by Thierry » Mon Feb 24, 2014 10:08 am

snop21 wrote: the string is =====> "(12) Boy90"
I will be getting ony the 12.
Hi Snop,

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
Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Mon Feb 24, 2014 10:15 am

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
Attachments
checkstringnumber.livecode.zip
sample
(1.06 KiB) Downloaded 193 times

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to get only the number in a string?.

Post by Thierry » Mon Feb 24, 2014 10:39 am

snop21 wrote: Thierry,
Thanks.! But there's a another way to do it.. not that complicated..
Come on, nothing complicated here. :)
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 :roll:

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.
!

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Mon Feb 24, 2014 10:51 am

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to get only the number in a string?.

Post by Thierry » Mon Feb 24, 2014 11:01 am

snop21 wrote: Hehe.. :))
Yah.. I remember one of my Prof. in Programming in College says "There a lot way of killing a chicken".
poor chicken :cry:

you're not English, as they are used to say:
there is more than one way to skin a cat
and in my country:
more than one way to cook a rabbit
So,who is right: the chicken, the cat or the 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.
!

snop21
Posts: 90
Joined: Mon Jan 20, 2014 2:54 pm

Re: How to get only the number in a string?.

Post by snop21 » Mon Feb 24, 2014 11:08 am

I am from the Philippines.. :) I am sorry if my english is not that good.. haha :lol:

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: How to get only the number in a string?.

Post by Thierry » Mon Feb 24, 2014 11:25 am

snop21 wrote:I am from the Philippines.. :)
Oh, a nice place I missed to visit..
I am sorry if my english is not that good..
Well, wasn't what I meant; I was intrigued only with the chicken :)
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.
!

Post Reply