Forums

Casting Strings to Integers

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Casting Strings to Integers

Postby creadak » Tue Jul 26, 2011 2:55 pm

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
creadak
 
Posts: 4

Re: Casting Strings to Integers

Postby Charles » Tue Jul 26, 2011 3:49 pm

testString = "12345"
testInteger = int.parse(testString)

You'll find .parse and .tryParse in the "shared" section of various types at the PrimitiveTypeMembers wiki page.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Casting Strings to Integers

Postby creadak » Wed Jul 27, 2011 12:34 am

That seems so obvious now, thanks a lot Charles :)
creadak
 
Posts: 4

Re: Casting Strings to Integers

Postby Charles » Wed Jul 27, 2011 11:32 am

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...
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 54 guests