- Home /
Finding the length of a string
How can i find out the character length of a string variable and then store that value in a integer variable, using javascript?
Answer by Eric5h5 · Mar 04, 2010 at 07:03 PM
var someString = "blah blah";
var stringLength = someString.Length;
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
Answer by Ashkan_gc · Mar 04, 2010 at 08:33 PM
you should use the Length property the string class.
var l = str.Length;
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).
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.
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 $$anonymous$$m if I'm right. ;)
Your answer
Follow this Question
Related Questions
Getting variable name from a string 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Edit Variables From Game! 1 Answer
GameObject.name to string 2 Answers