Detailed explanation of steps to use JS string method
This time I will bring you a detailed explanation of the steps to use the JSstring method, and what are the notes when using the JS string method. The following is a practical case, let's take a look.
1. Method reading instructions
Return value typeObject.Method name (parameter 1[, parameter two]);
Explanation:
返回值类型:指的是函数调用结束后返回的值的类型。 对象.方法名称:指的是调用方法。 参数列表:表示函数调用时传入的参数。[]表示可选参数,可写可不写。
2. String attribute
Definition: By a pair of "" or a pair of '' Wrapped, it is a string composed of 0 or more characters .
String length:
string.length;
eg:
var str1="abc"; var str2=""; var str3=" "; console.log(str1.length);//3 console.log(str2.length);//0 console.log(str3.length);//1
3.charAt
Function: This method returns the character at the corresponding position of .
Syntax: string string.charAt(index);
Parameters: index refers to 0 to string length-1 is an integer.
Return value: Returns the character at the corresponding position of the string.
Note:
- 如果传入参数小于0或者大于 字符串长度-1,则返回空字串。 - 如果传入boolean值,如果为true,默认是转化为数字1,指到字符串第二个字符。如果为false,默认是转化为数字0,指到字符串第一个字符。 - 如果传入任意字符串,则指到字符串第一个字符。
<script> var str="abc"; console.log(str.charAt(0));//a console.log(str.charAt(2));//c console.log(str.charAt(-88));//"" console.log(str.charAt(false));//a console.log(str.charAt(true));//b console.log(str.charAt("unm"));//a </script>
4.chatCodeAt
Function: Returns the Unicode value of the character corresponding to
Syntax: number string.charCodeAt(index);
Parameters: index refers to 0 to string length-1 is an integer.
Return value: Returns the Unicode value of the character at the corresponding position in the string.
Note:
If the incoming parameter is less than 0 or is greater than the string length -1, an empty string will be returned. NAN is returned.
<script> var str="abc"; console.log(str.charCodeAt(0));//97 console.log(str.charCodeAt(2));//99 console.log(str.charCodeAt(-88));//NAN console.log(str.charCodeAt(false));//97 console.log(str.charCodeAt(true));//98 console.log(str.charCodeAt(undefined));//97 console.log(str.charCodeAt("zzzz"));//97 </script>
4.fromCharCode
Function: Convert Unicode values into corresponding characters.
Syntax: string String.fromCharCode(index);
Parameters: index refers to passing in any integer.
Return value: Returns the string corresponding to the Unicode value.
<script> console.log( String.fromCharCode( 97 ) );//a console.log( String.fromCharCode( 65 ) );//A </script>
Small example of encryption and decryption
5.indexOf
Function: Returns the specified value string when calling this methodFirst timeThe position where it appears.
Syntax: number string.indexOf((searchValue [, fromIndex]));
Parameters: searchValue refers to The string to find. fromIndex refers to where to start searching. The default value is 0.
Return value: Returns a number.
Note: If it exists, the position will be returned, if it does not exist, -1 will be returned.
<script> var str="abcabcabc"; console.log(str.indexOf("a"));//0 console.log(str.indexOf("b"));//1 console.log(str.indexOf("z"));//-1 console.log(str.indexOf("ab"));//0 console.log(str.indexOf("ac"));//-1 console.log(str.indexOf("bc",0));//1 console.log(str.indexOf("bc",-2));//1 console.log(str.indexOf("bc",18));//-1 </script>
5.lastIndexOf
Function: Returns the specified value where the last of the string appears when this method is called.
Syntax: number string.indexOf((searchValue [, fromIndex]));
Parameters: searchValue refers to The string to find. fromIndex refers to where to start searching. The default value is str.length-1.
Return value: Returns a number.
Note: If it exists, the position will be returned, if it does not exist, -1 will be returned.
<script> var str="abcabcabc"; console.log(str.lastIndexOf("a"));//6 console.log(str.lastIndexOf("b"));//7 console.log(str.lastIndexOf("z"));//-1 console.log(str.lastIndexOf("ab"));//6 console.log(str.lastIndexOf("ac"));//-1 console.log(str.lastIndexOf("bc",0));//-1 console.log(str.lastIndexOf("bc",-2));//-1 console.log(str.lastIndexOf("bc",18));//7 </script>
6.slice
Function: method extracts a part of the string and returns this new string (including the starting position, not including End position)
Syntax: string string.slice((star [, end]));
Parameters: star is Refers to the interception starting position , end refers to the interception end position , the default is position 1 of the last character (the length of the string).
Return value: Return the intercepted string.
Note:
will not exchange parameter positions according to parameter size
If there are Negative values are processed from the end. -1 refers to the last element, -2 refers to the penultimate element.
<script> var str="abcabc"; console.log(str.slice(2));//"cabc" console.log(str.slice(0,2));//"ab" console.log(str.slice(2,2));//"" console.log(str.slice(2,-1));//"cab" console.log(str.slice(2,-6));//"" console.log(str.slice(2,1));//"" console.log(str.slice(-2,-1));//"b" </script>
7.substring
作用: 方法提取字符串中的一部分,并返回这个新的字符串(包含起始位置,不包含结束位置)
语法: string string.slice((star [, end]));
参数: star是指截取的起始位置,end是指截取的结束位置,默认为最后一个字符的位置+1 ( 字符串的长度 )。
返回值: 返回 截取后的字符串。
注意:
会根据起始位置和结束位置的大小先进行参数位置的变换
会把负值转换成0
<script> var str="abcabc"; console.log(str.substring(2));//"cabc" console.log(str.substring(0,2));//"ab" console.log(str.substring(2,2));//"" console.log(str.substring(2,-1));//"ab" console.log(str.substring(2,-6));//"ab" console.log(str.substring(2,1));//"b" console.log(str.substring(-2,-1));//"" </script>
8.substr
作用: 截取指定 起始位置和长度 的子字符串.
语法: string string.substr(start [, length]);
参数: start :截取的起始位置 。length:截取的字符串长度,默认为字符长度。
返回值: 返回截取后的字符串
<script> var str="abcabcabcabc"; console.log(str.substr(0));//abcabcabcabc console.log(str.substr(3));//abcabcabc console.log(str.substr(3,5));//abcab console.log(str.substr(3,-1));"" </script>
9.toLowerCase
1.toLowerCase
作用: 把字符串全部转成小写
语法: string string.toLowerCase();
返回值: 返回转成小写的字符串。
2.toUpperCase
作用: 把字符串全部转成大写
语法: string string.toUpperCase();
返回值: 返回转成大写的字符串。
<script> var str = "liangZhiFANG"; console.log( str.toLowerCase() );//"liangzhifang" console.log( str.toUpperCase() );//"LIANGZHIFANG" console.log( str );//"liangZhiFANG" console.log( "LoveJs".toLowerCase() );//"lovejs" </script>
10.split
作用: 通过一个指定的字符串 把原字符串分割成一个数组。
语法: array string.split([separator] [, limit])
参数:separator是指分割符。limit指定最多分割的数量,可以理解为数组长度,默认为全部。
返回值:返回一个数组。
注意:当没有分割符的时候(没有传入参数),整个字符串将作为一个整体保存到数组中。 用分割符分割的时候,分割符会在被删除了在传入数组。
<script> var str="我爱,你,们"; console.log(str.split(","));//["我爱","你","们"] console.log(str.split(",",2));//["我爱","你"] console.log(str.split());//["我爱,你,们"] console.log(str.split("mmm"));//["我爱,你,们"] console.log(str.split(""));//["我", "爱", "," , "你", "," ,"们"] </script>
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Detailed explanation of steps to use JS string method. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.
