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:
- var stringOne = "This is mic number one";
- var stringTwo = "Isn't this a lot of fun";
- if ( stringOne == stringTwo ) alert ( "Strings match" );
- 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:
- var stringOne = "This is mic number one";
- var stringTwo = "Isn't this a lot of fun";
- var stringThree = stringOne + ". " + stringOne + ". " + stringTwo + "!";
- 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.
- anchor()
- stringObject.anchor ( anchorName )
- big()
- stringObject.big ( )
- blink()
- stringObject.blink ( )
- bold()
- stringObject.bold ( )
- charAt()
- stringObject.charAt ( index )
- charCodeAt()
- stringObject.charCodeAt ( index )
- concat()
- stringObject.concat ( string1, string2, ... stringN )
- fixed()
- stringObject.fixed ( )
- fontcolor()
- stringObject.fontcolor ( color )
- fontsize()
- stringObject.fontsize ( size )
- fromCharCode()
- String.fromCharCode ( code1, code2, ... codeN )
- indexOf()
- stringObject.indexOf ( searchText [, index] )
- italics()
- stringObject.italics ( )
- lastIndexOf()
- stringObject.lastIndexOf ( searchText [, index] )
- link()
- stringObject.link ( targetURL )
- match()
- stringObject.match ( expression )
- replace()
- stringObject.replace ( expression [, replacementText|function] )
- search()
- stringObject.search ( expression )
- slice()
- stringObject.slice ( start [, end ] )
- small()
- stringObject.small ( )
- split()
- stringObject.split ( delimiter [, count] )
- strike()
- stringObject.strike ( )
- sub()
- stringObject.sub ( )
- substr()
- stringObject.substr ( start [, length] )
- substring()
- stringObject.substring ( start [, end] )
- sup()
- stringObject.sup ( )
- toLowerCase()
- stringObject.toLowerCase ( )
- toString()
- object.toString ( )
- toUpperCase()
- stringObject.toUpperCase ( )
- valueOf()
- stringObject.valueOf ( )
- Returns the value of stringObject as a primitive string. Equivalent to toString().