JavaScript String method

Source: Internet
Author: User

Character method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Character Methods </title>
<body>
<script type= "Text/javascript" >
/*
Both the Charat method and the charCodeAt method receive a parameter, based on the 0 character position
The Charat method is to return that character in a given position as a single-character string
The charCodeAt method obtains not a character but a character encoding.
*/
var str= "Hello World";
Console.log (Str.charat (1));//e
Console.log (Str.charcodeat (1));//101
You can also use square brackets and numeric indexes to access specific characters in a string
Console.log (str[1]);//e
</script>
</body>
String manipulation methods
Concat method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>concat Methods </title>
<body>
<script type= "Text/javascript" >
var str= "Hello";
var res=str.concat ("World");
Console.log (res);//hello World
Console.log (str);//hello This indicates that the value of the original string has not changed
var res1=str.concat ("Nihao", "!");
Console.log (res1);//hello nihao! Description Concat method can receive any number of parameters
Although the Concat method is dedicated to stitching strings, in practice we use the most or the plus operator +, because it's easy to do.
</script>
</body>
Slice method, substring method, substr method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> string Manipulation Methods </title>
<body>
<script type= "Text/javascript" >
/*
Slice method: The first parameter specifies the start position of the substring, and the second parameter represents the position after the last character of the substring
Substring method: The first parameter specifies the start position of the substring, and the second parameter represents the position after the last character of the substring
Substr method: The first parameter specifies the starting position of the substring, the second parameter represents the number of characters returned
All three methods return a substring of the manipulated string, receiving one or two parameters
If these methods are not passed a second argument, the length of the string is used as the end position. These methods also do not modify the string itself, just return a basic type of string value
*/
var str= "Hello World";
Console.log (Str.slice (3));//lo World
Console.log (Str.substring (3));//lo World
Console.log (STR.SUBSTR (3));//lo World
Console.log (Str.slice (3,7));//lo W 7 represents the position after the last character of the substring a simple understanding is that the containing header does not contain the tail
Console.log (str.substring (3,7));//lo W
Console.log (Str.substr (3,7));//lo worl 7 means return 7 characters

Console.log (Str.slice (3,-4));//lo w-4+11=7 indicates that the position after the last character of the substring is simply understood to include a header that contains no tail
Console.log (str.substring (3,-4));//hel will be converted to Console.log (str.substring (3,0));
Also, since this method will use the smaller number as the starting position and the larger number as the end position, it is equivalent to calling Console.log (Str.substring (0,3));
Console.log (Str.substr (3,-4));//"" empty string
Console.log (str.substring (3,0));
</script>
</body>
String Position method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> String Position method </title>
<body>
<script type= "Text/javascript" >
/*
Both the IndexOf method and the LastIndexOf method search for the given substring from a string, and then return the position of the substring, or 1 if not found.
The IndexOf method is to search the substring backwards from the beginning of the string, and the LastIndexOf method is just the opposite
Both methods can receive two parameters: the substring to find and the location to find it
*/
var str= "Hello World";
Console.log (Str.indexof ("O"));//4
Console.log (Str.lastindexof ("O"));//7
Console.log (Str.indexof ("O", 6));//7
Console.log (Str.lastindexof ("O", 6));//4
</script>
</body>
Trim method
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>trim Methods </title>
<body>
<script type= "Text/javascript" >
/*
The Trim method is used to delete spaces before and after a string
*/
var str= "Hello World";
Console.log (' (' +str.trim () + ') ');//(Hello World)
Console.log (' (' +str+ ') ');//(Hello World)
</script>
</body>

JavaScript String method

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.