Page 1 of 1

Casting Strings to Integers

PostPosted: Tue Jul 26, 2011 2:55 pm
by creadak
Hey all, new to Cobra and really really liking it.

Just a small problem I'm having is casting a String to an int. What I'm trying to do is something like this:

Code: Select all
testString = "12345"
testInteger = testString to int


Am I doing this completely wrong or is there method to do this?

I'm using Cobra-2010-10-18

Re: Casting Strings to Integers

PostPosted: Tue Jul 26, 2011 3:49 pm
by Charles
testString = "12345"
testInteger = int.parse(testString)

You'll find .parse and .tryParse in the "shared" section of various types at the PrimitiveTypeMembers wiki page.

Re: Casting Strings to Integers

PostPosted: Wed Jul 27, 2011 12:34 am
by creadak
That seems so obvious now, thanks a lot Charles :)

Re: Casting Strings to Integers

PostPosted: Wed Jul 27, 2011 11:32 am
by Charles
No problem. It inspired me to add a new error check for that situation in order to present the answer immediately:

Code: Select all
foo.cobra(4): error: Cannot cast a string to a numeric type. Consider using "int.parse" or "int.tryParse". Use "@help int" for details.

Works for casting a string to any numeric type. changeset:2582

See HowToInstallFromSource if you want the very latest Cobra.

I suppose the server should be building Cobra each night from the repository for easy downloading...