Sponsored Ad

Sunday, November 29, 2009

Working with Strings in JavaScript

What is a string?

A string is simply a number of characters (letters, numbers, spaces ) joined. For example, the following are all strings:

Hello

abc123

Wow!!!

A string with many words

 

Creating strings

Creating a string variable in JavaScript :

var myString = "This is mic number one";

Be sure to put quotes around the string. Use double quotes ( ") or single quotes ( '), but be sure to use the same contribution rate to the beginning and end!

The String object

JavaScript can really stores strings in two ways:

as a primitive string data type .

how you create the same string of an object.

var myString = new String ( "This is mic number one" );

Although JavaScript can store strings as primitive data types or objects, you rarely have to worry about this distinction. JavaScript will automatically convert to a string type of data to a String object as needed, and vice versa. As we shall see, which fortunately can be used in methods of the String object primitive strings, and JavaScript do not notice it.

Basic string operations


You can compare two strings to see if they match, as with the variables of number:





Code Snippet



  1. var stringOne = "This is mic number one";

  2. var stringTwo = "Isn't this a lot of fun";

  3.  

  4. if ( stringOne == stringTwo ) alert ( "Strings match" );

  5. if ( stringOne != stringTwo ) alert ( "Strings don't match" );






Of course, it shows an alert saying that "The strings do not match.

You can also join the chains, along with the + (concatenation) operator:




Code Snippet



  1. var stringOne = "This is mic number one";

  2. var stringTwo = "Isn't this a lot of fun";

  3.  

  4. var stringThree = stringOne + ". " + stringOne + ". " + stringTwo + "!";

  5. alert ( stringThree );





This shows an alert saying "This is the number one microphone. This is the number one microphone. Is not that a lot of fun!"

String properties

Strings in JavaScript only have one property that you need for everyday use: the length property. This tells how long the chain is in characters:


var myString = "These go to eleven."
alert ( myString.length );

String methods


There are many affairs you can do with strings in JavaScript. I nuniversal, do these things with the methods of the String object. Let's look at each of these methods in alphabetical order.




Code Snippet



  1. anchor()

  2. stringObject.anchor ( anchorName )

  3. big()

  4. stringObject.big ( )

  5. blink()

  6. stringObject.blink ( )

  7. bold()

  8. stringObject.bold ( )

  9. charAt()

  10. stringObject.charAt ( index )

  11. charCodeAt()

  12. stringObject.charCodeAt ( index )

  13. concat()

  14. stringObject.concat ( string1, string2, ... stringN )

  15. fixed()

  16. stringObject.fixed ( )

  17. fontcolor()

  18. stringObject.fontcolor ( color )

  19. fontsize()

  20. stringObject.fontsize ( size )

  21. fromCharCode()

  22. String.fromCharCode ( code1, code2, ... codeN )

  23. indexOf()

  24. stringObject.indexOf ( searchText [, index] )

  25. italics()

  26. stringObject.italics ( )

  27. lastIndexOf()

  28. stringObject.lastIndexOf ( searchText [, index] )

  29. link()

  30. stringObject.link ( targetURL )

  31. match()

  32. stringObject.match ( expression )

  33. replace()

  34. stringObject.replace ( expression [, replacementText|function] )

  35. search()

  36. stringObject.search ( expression )

  37. slice()

  38. stringObject.slice ( start [, end ] )

  39. small()

  40. stringObject.small ( )

  41. split()

  42. stringObject.split ( delimiter [, count] )

  43. strike()

  44. stringObject.strike ( )

  45. sub()

  46. stringObject.sub ( )

  47. substr()

  48. stringObject.substr ( start [, length] )

  49. substring()

  50. stringObject.substring ( start [, end] )

  51. sup()

  52. stringObject.sup ( )

  53. toLowerCase()

  54. stringObject.toLowerCase ( )

  55. toString()

  56. object.toString ( )

  57. toUpperCase()

  58. stringObject.toUpperCase ( )

  59. valueOf()

  60. stringObject.valueOf ( )

  61. Returns the value of stringObject as a primitive string. Equivalent to toString().




0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers