Javascript String Handling

Defines a String object

JavaScript String object for processing text strings. The syntax for creating a String object is as follows:

var str_object = new String(str);
var str1 = String(str);
var str2 = str;

Only the first method is to use the String constructor to strictly define a string object, it return object.

The second method is to call the String function, parameter str as a initial string and return.

The third method is to define a string variable, but the JavaScript is still in accordance with the string object to deal with.

Run the following statement to see the differences:

alert(typeof str_object);	// Output : object
alert(typeof str1);		// Output : string
alert(typeof str2);		// Output : string

String Object property

Properties Description
constructor Reference to the function that created the object
length the length of the string
prototype Add properties and methods to the object

String object methods

String objects commonly used methods are as follows:

String output

  • alert() : Warning message box text output
  • document.write() : the Web page output text

String operations

  • concat() : connecting two or more strings
  • replace() : string replacement or regular globs
  • slice() : to intercept the string by specifying the start and end positions
  • split() : string is divided into an array of strings
  • substr() : According to the interception of the string starting position and length
  • substring() : to intercept the string by specifying the start and end positions

String conversion

  • toLowerCase() : converts string to lowercase
  • the toUpperCase() : converts a string to uppercase
  • fromCharCode() : one or more Unicode value to a string

String search

  • charAt() : get the character at the specified position
  • charCodeAt() : get Unicode character encoding specified location
  • indexOf() : used to calculate the position of a specified string in the string for the first time
  • lastIndexOf() : used to calculate the last occurrence of the specified string in the entire string

HTML tag

  • bold() : get the character at the specified position
  • fontcolor() : the specified color to display the string
  • fontSize() : the specified size to display the string
  • italics() : string shown in italics
  • link() : String to add a hyperlink
  • strike() : given a string with a strikethrough
  • sub() : string displayed as subscript
  • sup() : string is displayed as superscript

Copyright 2014-2017 by All Rights Reserved.