JavaScript String Object (STRING) Basic Usage Example

This example describes the basic usage of the JavaScript string object (STRING). Share to everyone for reference, specifically:

1. Get the length of the string:

  VAR S = "Hello World"; Document.write ("Length:" + s.length;  
2. Add a variety of styles to strings, such as:

VAR txt = “Some Words”; Document.write (” BIG:” + TXT.BIG () + “
") Document.write (" small:" + txt.small () + "

") Document.write ("

bold:" + txt.bold () + "

") Document.write ("

italic:" + txt.italics () + "

") Document.write ("

Blink:" + TXT.BLINK () + "("

Fixed: "+" + ") Document.write ("

fixed: + TXT.FIXED () + "

") Document.write ("

strike:" + txt.strike () + "

") Document.write ("

fontcolor:" + TXT. FontColor ("Red") + "

") Document.write ("

fontsize:" + txt.fontsize (16) + "

")Document.write ("

Linked:" + TXT.LINK ("https://www.jb51.net") + "

")

3. Get part of the content in the string first appearance:

“VAR HW_Text =” Hello World “; Document.write (HW_Text.indexof (” Hello “) +”

“); Document.write (HW_Text.indexof (” World “) +”
“); Document.write (HW_Text.indexOf (” ABC “) +” “);
  
4. Content Replacement:

VAR STR = "Visit Microsoft!" Document.write (Str.Replace (/ Microsoft / , "W3SCHOOL"))

example code:
   

JavaScript string object

JavaScript字符串对象(string)基本用法示例

body {background-color: # e6e6e6}

 (1) Length attribute: Get the length of the string   Hello World, Hello JavaScript!   var s = document.getlementByid ("hw"). Innerhtml; Document.write ("Length:" + s.Length);   (2) When the string is added to the style   When the string call pattern is related to the method , Automatically splicing the corresponding HTML tag   Some Words   Call txt.big () 

var txt = document.getlementById ("hw_02"). InnerHtml; Document. Write ("

BIG:" + TXT.BIG () + "

") Document.write (" small:" + txt.small () + "") Document.write "

bold:" + txt.bold () + "

") Document.write ("

italic:" + txt.italics () + "

") Document.write ("["

BLINK: "+ TXT.BLINK () +" Document.write ("

fixed:" + txt.fixed () + " strike:" + txt.strike () + "") Document.write ("

fontcolor:" + TXT.FONTCOLOR ("RED") + "

") Document.write ("

fontsize:" + txt.fontsize (16) + "

") DOCUMENt.write ("

link:" + txt.Link ("https://www.jb51.net") + "

") function alertbig () {alert (txt.big ());}

(3) IndexOF method: The position of a specified character in the locker string

var hw_text = "Hello World"; Document.write (HW_Text.indexof ("Hello" ) + "

"); Document.write ("World") + "

"); document.write (hw_text.indexof ("ABC") + "

");

(4) REPLACE () Method: Replace the part of the string

Var str = "Visit Microsoft!" Document.write (Str.Replace /, "JB51")

More about JavaScript-related content, readers are interested in this site: "JavaScript Replace Skills Summary", " JavaScript Finding Algorithm Skill Summary "
I hope this article will help you JavaScript programming.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment