Saturday, November 3, 2012

concatenating strings in javascript

concatenating strings in javascript

http://www.quirksmode.org/js/strings.html


You can even use numbers or calculations, like:
document.write (a + 3*3 + b);
Now we concatenate string a, then the result of 3*3, treated as a string for the occasion, and then b, which gives
Hello world!9I am a JavaScript hacker.
Watch out if you want to add numbers. This
document.write (a + 3+3 + b);
concatenates four strings: a, 3, 3 and b, because + in this context meansconcatenate strings, not add, and gives
Hello world!33I am a JavaScript hacker.
Use brackets if you want to add 3 and 3 before making the string.
document.write (a + (3+3) + b);
concatenates a, the result of 3+3 and b and gives
Hello world!6I am a JavaScript hacker.

indexOf

One of the most widely used automatical methods is indexOf. Each character has an index number, giving its position in the string. Note that the first character has index 0, the second one index 1 etc. So the index of 'w' in string a is 6.
Using indexOf we can read out the index number of a character. Put the.indexOf('') behind the string name and put the character you're looking for between the quotes. This
var a = 'Hello world!';
document.write(a.indexOf('w'))
gives 6. If the same character occurs more than once, this method gives the first occurrence. So






No comments:

Post a Comment