JS String Manipulation method

Source: Internet
Author: User

1. Character Method:

Str.charat (): can access a specific character in a string, can accept a number of 0 to a string length-1 as a parameter, return the character under that position, if the argument exceeds the range, returns an empty string, if there is no argument, returns the position of 0 characters;

Str.charcodeat (): As with Charat () usage, the difference is that charCodeAt () returns a character encoding instead of a character.

var  anystring= "Hello Tino"; Anystring.charat ()   //"H"Anystring.charat (2)   //  "L"Anystring.charat (+)   //""

2. String manipulation methods:

string concatenation:

The most common is the string concatenation, you can use the "+" operator to connect two strings, or use the concat () method, the Concat () method to accept one or more string parameters, return the concatenation of the resulting new string, it should be noted that the concat () method does not change the value of the original string.

var str= "Hello Tino"; var nstr=str.concat ("and Nick");   Console.log (str);   // "Hello Tino"console.log (NSTR);  // "Hello Tino and Nick"

Create a new string based on a substring:

Slice () accepts one to two arguments, the first argument is the start of the specified string, the second argument is where the string ends (the returned string does not include the location), and if there is no second argument, the end of the string is the ending position. If no arguments are passed, the original string is returned. The slice () method does not change the original string

var str= "Hello Tino"; Str.slice (5,9)    //  "Tino"Str.slice (5)      // "Tino"Str.slice ()       //"Hello Tino"

The substr () method, like the slice () method, differs in that when the parameter is negative, the slice () method adds the passed-in negative value to the string length, and substr () adds the first negative value and the second negative argument to 0;

var str= "Hello Tino"; Console.log (Str.slice (-4));       // "Tino"Console.log (Str.substr ( -4));      // "Tino"Console.log (Str.slice (1,-4));     // "ell"Console.log (Str.substr (1,-4));    //  ""

The substring () method also accepts two parameters, different from the first two, and substring () the second parameter is the number of characters represented.

JS String Manipulation method

Related Article

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.