This code is compiled from W3school:http://www.w3school.com.cn
1. Get the length of the string:
var s = "Hello World";d ocument.write ("Length:" +s.length);
2. Add a variety of styles to the string, such as:
var txt = "Some words";d ocument.write ("<p>big:" + txt.big () + "</p>") document.write ("<p>small:" + txt.) Small () + "</p>") document.write ("<p>bold:" + txt.bold () + "</p>") document.write ("<p>italic:" + txt.italics () + "</p>") document.write ("<p>blink:" + txt.blink () + "(does not work in IE) </p>") Docume Nt.write ("<p>fixed:" + txt.fixed () + "</p>") document.write ("<p>strike:" + txt.strike () + "</p> ") document.write (" <p>fontcolor: "+ txt.fontcolor (" Red ") +" </p> ") document.write (" <p>fontsize: "+ Txt.fontsize (+) + "</p>") document.write ("<p>link:" + txt.link ("http://www.w3school.com.cn") + "</p > ")
3. Get the location where some content in the string first appears:
var hw_text = "Hello World";d ocument.write (Hw_text.indexof ("hello") + "<br/>");d Ocument.write (Hw_text.indexof ( "World") + "<br/>");d Ocument.write (Hw_text.indexof ("abc") + "<br/>");
4. Content Replacement:
var str= "Visit microsoft!" document.write (Str.replace (/microsoft/, "W3school"))
:
Example code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Javascript: The basic operation of a String object (String)