|
How can i find out the character length of a string variable and then store that value in a integer variable, using javascript?
(comments are locked)
|
Hey Eric thankyou for this. the first answer was a little confusing because of the use of "str". Your answer is much less confusing for me, a lone wonderer through the answers of Unity.Answers
Dec 22 '10 at 07:54 AM
dissidently
(comments are locked)
|
|
you should use the Length property the string class.
it will return an int so the variable in lefthandside will become an int because js is a dynamic language. infact the string class in so powerful but unity guys did not document it. take a look at the real documentation of hte class here. String is not a unity class and is the System.String class of .NET/MONO. see the Length property too. The left hand variable doesn't become an int because JS is a dynamic language, it becomes an int because of type inference. C# 3.0 does the same thing. (In fact, JS in Unity is primarily statically-typed, and becomes completely so if you use #pragma strict).
Mar 04 '10 at 08:49 PM
Eric5h5
oops you are right because i defined the variable here. you are wrong in the other side. the js language of unity is a dynamic language and it will become strictly typed if you use pragma strict. var keyword in C# 3 is different and there is no dynamic feature outthere. you just say the compiler that i will define the type of this variable the frst time that i assign something to it. you can not change it later by any means. in C# 4 you can use dynamic keyword to use new dynamic features of C# language.
Mar 04 '10 at 09:28 PM
Ashkan_gc
No, I am not wrong. JS is statically typed when you use statements like "var l = str.Length;", because of type inference. You cannot change the type of the "l" variable later; it will always be an int. This is the same type inference that C# 3.0 has. JS only uses dynamic typing when the compiler can't figure out the type. Feel free to ask the Unity team if I'm right. ;)
Mar 04 '10 at 10:43 PM
Eric5h5
(comments are locked)
|
