String manipulation functions in JavaScript

Source: Internet
Author: User
Tags first string

1. The charCodeAt method returns an integer that represents the Unicode encoding of the specified position character.
Strobj.charcodeat (Index)
Description
Index The zero-based number of the character to be processed. A number with a valid value of 0 to a string length minus 1.
If there are no characters at the specified location, Nan is returned.
For example:

The code is as follows Copy Code
var str = "ABC";
Str.charcodeat (0);

Results: 65
2. The fromCharCode method returns a string from some Unicode strings.
String.fromCharCode ([Code1[,code2 ...]])
Description
Code1,code2 ... is the sequence of Unicode strings to convert to a string. If there are no arguments, the result is an empty string.
For example:

The code is as follows Copy Code
String.fromCharCode (65,66,112);

Result: ABp
3. The Charat method returns the character at the specified index position. Returns an empty string if the index value exceeds the valid range.
Strobj.charat (Index)
Description
The zero-based index of the character that Index wants. A valid value is a value of 0 minus the length of the string.
For example:

The code is as follows Copy Code
var str = "ABC";
Str.charat (1);

Result: B
4, the Slice method returns a fragment of the string.
Strobj.slice (Start[,end])
Description
The start subscript strobj The specified portion of the index from 0. If start is negative, it is treated as length+start, where length is the length of the string.
End Strobj Specifies a portion of the index from the beginning of the 0. If end is negative, it is treated as length+end, where length is the length of the string.
For example:

The code is as follows Copy Code
012345
var str = "ABCDEF";
Str.slice (2,4);

Result: CD
5. The substring method returns a substring at the specified position in a string object.
Strobj.substring (Start,end)
Description
Start indicates the starting position of the substring, starting at 0.
End indicates the ending position of the substring, starting at 0.
The substring method uses the smaller values in both start and end as the starting point for substrings. If start or end is Nan or is a negative number, replace it with 0.
For example:

The code is as follows Copy Code
012345
var str = "ABCDEF";
Str.substring (2,4); or str.substring (4,2);

Result: CD
6. The Substr method returns a substring of the specified length starting at the specified position.
Strobj.substr (Start[,length])
Description
The starting position of the substring required for start. The index of the first character in the string is 0.
Length The number of characters that should be included in the returned substring.
For example:

The code is as follows Copy Code
012345
var str = "ABCDEF";
STR.SUBSTR (2,4);

Result: Cdef
7. The IndexOf method puts the substring position into the string object for the first time. If no substring is found, returns-1.
Strobj.indexof (Substr[,startindex])
Description
SUBSTR a substring to find in a string object.
startindex This integer value indicates the index in which to start the lookup within a string object. If omitted, it is looked up from the beginning of the string.
For example:

The code is as follows Copy Code
01234567
var str = "ABCDECDF";
Str.indexof ("CD", 1);

Find 123 from left to right from 1 bits ...
Results: 2
8. The LastIndexOf method returns the last occurrence of a string in a string object. Returns-1 if there is no match to the substring.
Strobj.lastindexof (Substr[,startindex])
Description
SUBSTR a substring to find within a string object.
startindex This integer value indicates the starting index position of the lookup within a string object. If omitted, the lookup starts at the end of the string.
For example:

The code is as follows Copy Code
01234567
var str = "ABCDECDF";
Str.lastindexof ("CD", 6);

Find from right to left from 6 position ... 456
Results: 5
9. The search method returns the position of the first string that matches the lookup content of the regular expression.
Strobj.search (REEXP)
Description
Reexp a regular Expression object that contains the regular expression pattern and the available flags.
For example:

The code is as follows Copy Code
var str = "ABCDECDF";
Str.search ("CD"); or Str.search (/cd/i);

Results: 2
10. The Concat method returns a string value that contains the connection of two or more supplied strings.
Str.concat ([string1[,string2 ...]])
Description
String1,string2 a String object or literal that you want to connect to all other specified strings.
For example:

The code is as follows Copy Code
var str = "ABCDEF";
Str.concat ("ABCDEF", "ABC");

Result: ABCDEFABCDEFABC
11. Splits a string into substrings and returns the result as an array of strings.
Strobj.split ([Separator[,limit]])
Description
Separator a string or regular expression object that identifies whether one or more characters are used to delimit a string. If this option is omitted, an array of single elements containing the entire string is returned.
Limit this value is used to limit the number of elements returned in an array.
For example:

The code is as follows Copy Code
var str = "AA BB CC DD EE FF";
Alert (Str.split ("", 3));

Results:
Aa,bb,cc
12. The toLowerCase method returns a string in which the letters in the string are converted to lowercase.
For example:

The code is as follows Copy Code
var str = "ABCABC";
Str.tolowercase ();

Result: ABCABC
13. The toUpperCase method returns a string in which all the letters in the string are converted to uppercase letters.
For example:

The code is as follows Copy Code
var str = "ABCABC";
Str.touppercase ();

Result: ABCABC

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.