6 - Javascript:Objects - The String object
A string is one or more words or characters in a row. It can also be thought of as any number of characters in a single unit.
The following are all strings:
"I am a string"
"is"
"a b c d e"
"000100149911"
The following are NOT strings:
1
15125
So what's the difference?
Remember, even numbers can be a string if they are held within quotes or are transformed into that format explicitly with something such as an input box or a conversion function. So, don't confuse the number 13 with the string "13" they are two different things (e.g. one could add "13" and "13" but you would either get "1313" or some sort of nonsense; however, you would most certainly not get 26)
The String's Length Property
A strings length property tells us how long the string is, in characters. For instance, "12345" is a string that is five characters long. "This is a string" is a string that is 16 characters long. So that shows us that even spaces are counted as characters.
To access the length property, we can use "dot-notation".
foo.length
Also, we need to provide some place, another variable probably, to place the answer returned to us. So:
foo = "November"
intLength = foo.length
alert("The length of the string is: " + intLength)
//or this does the same thing and is a bit shorter to boot (good for //when
you don't need to keep the length around in another variable)!
foo = "November"
alert("The length of the string is: " + foo.length)
So, what use is this? Well, what if we needed to know whether or not a user
entered a long enough entry to be a phone number or a zip code? We can use 5
characters but not 3. The length property easily tells us the input is too short.
Also, maybe we need to do perform some action once for each character in our
string. The length property allows us to do something the right number of times
without knowing how the long the string is beforehand.
toLowerCase & toUpperCase methods
It is possible to change the letters in a string to either all lower or upper case no matter what they are now.
So, say we place a string into a variable then execute the toLowerCase method:
foo = "Here is a String to Play With"
newfoo = foo.toLowerCase()
newfoo will now contain the string "here is a string to play with".
Likewise, if we execute the toUpperCase() method with this similar code:
foo = "Here is a String to Play With"
newfoo = foo.toUpperCase()
newfoo will contain "HERE IS A STRING TO PLAY WITH"
Please note that the string in foo does not change. The string within it is converted and then returned by the method just like it would be using a function (because methods are like functions you can write but they are built into the object!). These methods do not change to contents of foo at all. So, remember you will need to have another variable ready to take the new string after the call is done.
Try it yourself:
These methods are great for making case-insensitive searches. If a user enters "Dog" as the term to search on and doesn't care about searching on the case of the letters, then call either toLowerCase() or toUpperCase() on both the users data and the data you are searching. That way the user will get matches to "dog", "DOG" and "Dog" among others. In effect, it will be as though both the user entered "DOG" and all the data is "DOG" even though that might not be the case.
indexOf method
What if you want to find a string within another string? That is easily accomplished using the indexOf method.
First, we create a new string object then say we want to find the word "new" within the string contained within the string object. We would use the indexOf method to accomplish this task. What indexOf does is return to us the starting position of the string we looked for within the string we searched. Watch:
If the word we wish to find is "new" then we would write the code as such:
foo = "I am a new string"
theResult = foo.indexOf("new")
The variable we provided, "theResult", will now contain the index (location) of "new" inside of "I am a new string". If indexOf fails to find what it looked for it returns -1 to let us know.
Hint: the indexOf method is case-sensitive
so "Cat" is not the same as "cat"
This can be used as a simple check on a field where a user is supposed to enter an email address:
This is just an intro to the String object. All its properties and methods can be found here.
(from jimparshall.net)