Javascript: The basic operation of a String object (String)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.