Home > Web Front-end > JS Tutorial > body text

Summary of common usage methods of JavaScript string classes_javascript skills

WBOY
Release: 2016-05-16 16:04:24
Original
1197 people have browsed it

Get class:

1) Dynamic method:

charAt: Get the character at the specified position in the string. (Parameter: one, specify the character position to be obtained)

1, negative numbers are not accepted. If it is a negative number, an empty string will be returned.

2, if no parameters are given, the character at position 0 is obtained by default.

3, only receives one parameter.

charCodeAt: Get the Unicode encoding of the character at the specified position in the string (parameter: one, specify the character position to get the character encoding)

1. Any character has a unique character encoding.

2, only receives one parameter.

Commonly used:

Number: 48 ~ 57

Underscore: 95

Space: 32

Tab character: 9

Lowercase letters: 97 ~ 122

Capital letters: 65 ~ 90

2) Static method:

fromCharCode : Returns the corresponding character according to the specified character code. (Parameter: any number)

1, can receive multiple parameters.

2. The writing method is fixed (static method): String.fromCharCode(); //Character encoding valid range: 0 ~ 65535 String is a string object

 var str = '我是字符串';
 alert( str.charAt( 2 ) );  //''  如果本身长度只有5,却找str.charAt(12) 找不到也是空字符串'',0~str.length-1是合法范围。
 alert( str.charAt( ) );   //'我' 默认不写是0,找到第一个字符
 alert( str.charAt(2) );   // '字'
 alert( '1234'.charAt( 2 ) ); //3
 alert( '1234'.charAt(2,3) ); //3
 alert( str.charCodeAt( 2 ) );//23383 unicode编码
 alert( '1'.charCodeAt() );  // 49
 alert(String.fromCharCode(23383 ,21619)); //'字味' 根据编码转换成字符(静态方法)多个字符用 , 隔开

Copy after login

Search class:

indexOf : Finds the first occurrence of the specified substring in the string. (The first parameter specifies the substring to be searched; the second parameter specifies the position to start searching.)

1, search from front to back, starting from position 0 by default.

2. If found, return the first found position. If not found, return -1.

3, if the second parameter is negative, it will be treated as 0 by default

lastIndexOf : Find the last occurrence of the specified substring in the string. (The first parameter specifies the substring to be searched; the second parameter specifies the position to start searching.)

1, search from back to front, starting from the length - 1 position by default.

2. If found, return the first found position. If not found, return -1.

 var str = 'www.baidu.com/2015';
 alert(str.indexOf('bai'));  //4 从左往右找找到一个就返回不会再往右找了
 alert(str.indexOf('m',5))  //12 从第5位开始向右找
 alert(str.indexOf('X'))   //-1 不存在的话结果是 -1 表示没找到
 alert(str.lastIndexOf('ww')); //1

Copy after login

Interception class:

substring : Extract a substring in the specified range. (The first parameter specifies the starting position to be extracted; the second parameter specifies the end position to be extracted.)

1, the extraction range includes the starting position, but does not include the ending position.

2, you can omit the second parameter, which means extracting from the starting position to the end of the string

3. Before extraction, the sizes of the two parameters will be compared first, and then the parameter positions will be adjusted in order from small to large, and then extracted.

4, all illegal parameters will be automatically converted to 0.

5, if no parameters are given, the entire string will be returned directly by default.

slice : Extract a substring in the specified range. (The first parameter specifies the starting position to be extracted; the second parameter specifies the end position to be extracted.)

1, the extraction range includes the starting position, but does not include the ending position.

2, you can omit the second parameter, which means extracting from the starting position to the end of the string

3, will not compare the positions of the two parameters, let alone adjust the position

4, the parameter can be a positive number or a negative number, and all other illegal parameters will be converted to 0.

5, a negative number indicates the character position from the back of the string to the front, and the position of the last character is -1.

 var str = '我是字符串';
 alert(str.substring());   //'我是字符串'
 alert(str.substring(-2,1)); //'我'
 alert(str.substring(2));  //字符串
 alert(str.substring(0,2)); //'我是' 与str.substring(2,0)是一样的。可以检测两个数,大的往后仍,小的往前仍。负数当成0来处理。
 alert(str.slice(2,0));    //空白 找不到,不交换位置
 alert(str.slice(-2));     //'符串' 负数就是从后面倒着往前数

Copy after login

Comparison type:

alert('I'>'you'); //true String comparison only compares the Unicode value corresponding to the first character, and the following characters are not compared.

Other categories:

alert(str.length); //Get the string length

split() //Cut the string into an array

Parameters: a specifies a delimiter used to split the string.

1. If the separator is not specified, it will not be divided and will be stored directly in the array.

2. Based on the separator, store the values ​​on the left and right sides of the separator into arrays.

3. The separator itself will not be stored in the array.

4. The delimiter can only be a substring that exists in the string.

5. In the view of split, two characters must be connected by an empty string.

6. When using an empty string to split an empty string, you will get an empty array.

var str = '1234';
alert( typeof str ); //string
alert( typeof str.split() ); //object
alert( str.split().length ); //['1234']
alert( str.split('2') ); //['1','34']
alert( str.split('a') ); //['1234']
alert( str.split('23') ); //['1','4']
alert( str.split('').length ); //['1','2','3','4']
alert( str.split('') );    //['1','2','3','4']
//'1234' 由五个 '' 加 四个字符组成
alert( str.split('1234') );//['','']
alert( str.split('1') );  //['','234']
alert( ''.split(' ').length ); //['']
alert( ''.split('').length ); //[]
//特例,只有这种情况下 split 才会返回空数组。
Copy after login

trim(): Remove all spaces at the beginning and end of the string. (Spaces in the middle of the string will be preserved).

​​ ​​ html5’s new method is not supported by lower version browsers.

toUpperCase(): Convert all strings to uppercase. (no parameters)

toLowerCase() : Convert all strings to lowercase. (no parameters)

Finally, all string methods will not modify the string itself.

The above is the entire content of this article, I hope it can be helpful to everyone.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template