A detailed description of common JavaScript string methods

Source: Internet
Author: User
Tags string methods

String

A string is one or more characters arranged together, placed in single or double quotation marks.

' ABC '
"ABC"

Length Property

The string in JS is similar to an array, which is made up of a single character, so the length of the string can be obtained with the length property.

var str = "Hello"
Str.length; 5

Some common methods of string

1. CharAt ()

1 str.charat (n)

= = Returns the nth character of a string, or an empty string if it is not between 0~str.length-1.

1 var str = "JavaScript"; 2 // ' V ' 3 // '

2. IndexOf ()

1 indexOf (Substr[,start])

= = Returns the position of the first occurrence of the substr in the string str, starting from the start position, or 1 if it does not exist.

Start can be any integer and the default value is 0. If start < 0 finds the entire string (as if it were passed in 0). If start >= str.length, the method returns-1 unless the string being looked up is an empty string, which returns STR.LENGTH.

1 var str = "JavaScript"; 2 // 1 3 // -1 4 // Ten 5 // 8

3. LastIndexOf ()

1 lastIndexOf (Substr[,start])

= > Returns the position of the last occurrence of the substr in the string str, starting from the start position, and returns 1 if it does not exist.

1 ' lastindex '. LastIndexOf (' a '); // 1

4. SUBSTRING ()

1 str.substring (start[, end])

= > Returns the character from start to end (not included), and start and end are non-negative integers. If the end parameter (end) is omitted, it is truncated from the start position to the last.

1 var str = ' ABCDEFG '; 2 // "BCD" 3 // "BCDEFG" 4 // "ABCDEFG" is treated as 0 when a negative value is passed in

5. Slice ()

1 str.slice (Start[,end])

= > Returns the character from start to end (not included), which can be passed negative

1 var str = ' This is awesome '; 2 Str.slice (4,-1); // "is Awesom"

6. substr ()

1 str.slice (Start[,end])

= > Returns a substring from the specified position in STR to a specified length, start can be negative

1 var str = "Just give me a Reason"; 2 // "Give me a" 3 // "as"

7. Replace ()

1 str.replace (regexp|substr, newsubstr| function)

= > Replace substring of str

1 var str = "Do It Love Me"; 2 str.replace (' Love ', ' hate '); // "Do you Hate Me"

8. Search ()

1 str.search (regexp)

= > Finds if str matches a regular expression. If the match succeeds, the index of the first occurrence of the regular expression in the string is returned; otherwise, 1 is returned. If the parameter passes in an non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)

1 var str = ' I love javascript! ' ; 2 // -1 3 // 7 4 // 7 5 // 7

9. Match ()

1 str.match (regexp)

= > Returns an array containing the matching results, or null if there are no matches. If the parameter passes in an non-regular expression object, it is implicitly converted to a regular expression object using new RegExp (obj)

1 var str = ' Javascript java '; 2 // ["Java"] 3 // ["Java", "Java"] 4 // NULL

Split ()

1 str.split ([separator][, limit])

= > Returns an array where the delimiter separator can be a string or regular expression

1 var str = "Hello?" world! " ; 2 // ["Hello? world! "] 3 // ["H", "E", "L", "L", "O", "?", "W", "O", "R", "L", "D", "!"] 4 // ["Hello", "world!"] 5 // ["H", "E", "L", "L", "O"]

Trim ()

1 str.trim ()

= > Remove white space characters at the beginning and end of STR, returning a copy of STR without affecting the value of the string itself

1 var str = ' abc '; 2 // ' abc ' 3 // ' abc '

toLowerCase ()

1 str.tolowercase ()

= > Converts str to lowercase and returns a copy of STR without affecting the value of the string itself

1 var str = ' JavaScript '; 2 // ' JavaScript ' 3 // ' JavaScript '

toUpperCase ()

1 str.touppercase ()

= > Converts str to uppercase and returns a copy of STR without affecting the value of the string itself

1 var str = ' JavaScript '; 2 // ' JAVASCRIPT ' 3 // ' JavaScript '

A detailed description of common JavaScript string methods

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.