Java Script string

Source: Internet
Author: User

Java Script string

Various methods to deal with existing word converts sequential blocks

1. Returns the character at the specified position

Example: var test = ' STT ';                 document.write (Test[0]); The result is s;

2. Calculating the length of a string

Example: var test = ' STT ';               document.write (test.length); The result is 3;

3. Return the original value of the object

Example: var test = new String (' STT ') document.write (test.valueof ()); The result: STT;

4.charAt () returns the character at the specified position

Example: var test = ' Hello World ' document.write (Test.charat (7)); The result is O;

Note the first character subscript is 0;

5.charCodeAt () returns the ASCII code value of the specified character

Example: var test = ' ABCs '; document.write (Test.charcodeat (2)); The result is 99;

Explanation: Returns the ASCII code value of the character labeled 2

6.fromCharCode () returns the ASCII corresponding character

Example: document.write (String.fromCharCode (99)); The results were: C;

7.concat () connection string

Example: var test = ' Xiao ';        document.write (Test.concat (' Ming ')); The result is xiaoming;

Note: String types can resolve spaces, and commas without quotes in the output are resolved to spaces

8.indexOf () Retrieving strings

Example: var test = ' xiaoming ' document.write (Test.indexof (' m ')); The result is: 4

Explanation: The parameter of IndexOf () returns a value for the string where the representation finds the string when the string is not found-1

Note: the Cahrat () parameter is a string that represents the position of the string at which the position is returned INDEXOF () is the location where the string is found to return

Extension: Counts the number of occurrences of a character in a specified string by combining indexof () with Loop Whlie () {}

Example: var test = qqwsssssttwetsdfe;

var count = 0;

         var pos=str.indexof (' s ');

         while (Pos!=-1) {

           count++;

pos=str.indexof (' s ', pos+1);

         }

9.lastIndexOf () retrieving strings from backward forward

Example: var test = ' xiaomingm ' document.write (Test.indexof (' m ')); The result is: 8

Note: Returns the subscript or the original subscript of a string when retrieving from backward forward

10.localeCompare () Comparison of two string ASCII values

Example: document.write (' a '. Localecompare (' B ')); The results were:-1;

Explanation: Returns 1 when the ASCII value of the parameter is greater than the comparison character; returns 1 if it is less than 0

Match () finds the matching of one or more regular expressions

Example: var test = ' Jin Tian Chi de sm an ';

var re =/an/i This is a regular expression where an is to match the characters I represent case insensitive also can write G for global match

Document.wrote (Test.match (re));

Explanation: The return value is an array object array containing the position of the character to match the original string

var re =/an/ig

document.write (Test.match (re)); Case insensitive and global matching

Explanation: The return value array includes the characters to match the occurrences of several stores and the location of each

12.search (): Search by regular expression

Example var test = ' Jin Tian Chi de sm an ';

document.write (Test.search (/ch/)); The result is: 10

Explanation parameter is a regular expression returns a value that is the subscript to match the character

Note that the return value of search () differs from match ()

13.replace () replaces the substring that matches the regular expression.

var test = ' AB ';

var string = Test.replace (/b/ig, ' C ');

document.write (string); The result is AC;

Explanation: Replacing a character found with a regular expression with the second argument the first argument is a regular expression the second is the character to be replaced

Complex usage: var str= "2015-09-26";

var newstr=str.replace (/(\d{4})-(\d{2})-(\d{2})/, "$2/$3/$1");
Console.log (NEWSTR); The results were: 09/26/2015;

   Explanation: Match-first n-length characters and transform sequential output

14.slice () Intercept string

Example: var str= ' abcdef ';

Console.log (Str.slice (0,2));

Explanation: Starting with the character subscript 0, the character ending with a subscript of 2 does not include the character with the subscript 2.

15.split () splits a string into an array

Example: var str= ' A b c d e F ';

    var arr=str.split (', 3);

    Console.log (arr);

     To divide a character into an array the first parameter represents the length of the split-boundary by what the second parameter represents

Java Script 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.