JavaScript Basics String Method

Source: Internet
Author: User


1: Property length
is to get the length of the string
Note: Chinese, digital, English letters, spaces, are 1 lengths
eg:
"Happy Camp Oh". Length
//8
var str = ' abc '. length;
//3
2: Attribute prototype
adds a property or method to an object (String), and adds a method or property that is generic on all instances
eg:
String.prototype.trim = function () {
return This.replace (/(^\s*) | ( \s*$)/g, "");
}
3:concat
concatenate two or more strings
The original string does not change and returns a new string
Stringobject.concat (Stringx,stringx,..., stringx);
var str1= "Hello"
var str2= "world!"
var newstr=str1.concat (str2);
Console.log (str1);
Console.log (str2);
Console.log (NEWSTR);
//hello
//world!
//hello world!
4:charat
returns the character at the specified position, starting at 0. If index is a non-existent number, an empty string is returned.
Stringobject.charat (Index)
eg:
"ABCdef". CharAt (1);
//"B"
5:charcodeat
returns the Unicode encoding of the character at the specified position, or a Nan If index is a number that does not exist.
stringobject.charcodeat (Index)
eg:
var str= "Hello";
Console.log (Str.charcodeat (2))
//108
5:slice
method to intercept a portion of a string and return the intercepted part with a new string.
Stringobject.slice (start,end)
starting with the start subscript, excluding end
end does not just extract to the last.
eg:
var str= "Hello Happy world!"
var newstr=str.slice (6);
Console.log (NEWSTR);
Console.log (str);
//happy world!
//hello Happy world!
6:substring
extracts a character in a string between two specified subscripts
starting with the start subscript, excluding end
Note: Start is equal to stop and returns an empty string.
start is larger than stop, the two parameters are exchanged before the string is extracted.
stringobject.substring (start,stop)
var str= "Hello world!";
var newstr=str.substring (3);
Console.log (NEWSTR);
Console.log (str);
//lo world!
//hello world!
eg:
var str= "Hello world!"
var newstr=str.substring (7,3);
Console.log (NEWSTR);
Console.log (str);
//lo W
//hello world!
7:substr
extracts a specified number of characters from the start subscript in a string.
stringobject.substr (start,length)
eg:
var str= "Hello world!"
var newstr=str.substr (3);
Console.log (NEWSTR);
Console.log (str);
//lo world!
//hello world!
8:indexof
a specified string value Searchvalue where it first appears in the string.
Stringobject.indexof (Searchvalue,fromindex)
Fromindex refers to the position in the string where the search begins.
the retrieved string value does not appear, returning-1.
var str= "Hello world!"
var newstr=str.indexof (' ll ');
Console.log (NEWSTR);
//2
9:lastindexof
a specified string value searchvalue the last occurrence in the string.
Stringobject.lastindexof (Searchvalue,fromindex)
var str= "Hello world!"
var newstr=str.lastindexof (' W ');
Console.log (NEWSTR);
//6
10:search
get the position of a character in a string
Stringobject.search (regexp)
no matching substrings were found, 1 is returned.
do not perform a global match
var str= "Hello world!"
var newstr=str.search (/world/);
Console.log (NEWSTR);
//6
splits a string into a string array.
note "and";
var str= "Hello World"
var strarr=str.split ("");
Console.log (Strarr);
//["Hello", "World"]
var str= "Hello,world"
var strarr=str.split (",");
Console.log (Strarr);
//["Hello", "World"]
var strarr= "Hello". Split ("");
Console.log (Strarr);
//["H", "E", "L", "L", "O"]
11:replace
string Substitution
eg:
"ABCD". Replace ("a", "0");
//"0BCD"
eg:
var str= "Hello world!"
Console.log (Str.replace (/world/, "W"))
//hello w!
12:tolowercase
Convert a string to lowercase
stringobject.tolowercase ()
The original string is not changed, and a string is returned
var str= "Hello world!"
var newstr=str.tolowercase ();
Console.log (str);
Console.log (NEWSTR);
//hello world!
//hello world!
13:touppercase ()
Convert a string to uppercase
stringobject.touppercase ()
The original string is not changed, and a string is returned
var str= "Hello world!"
var newstr=str.touppercase ();
Console.log (str);
Console.log (NEWSTR);
//hello world!
//hello world!

JavaScript Basics 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.