String Retrieval Method

3-1 String (String Retrieval Method)


String Retrieval Method

1. Want to get a character in a string

stringObject.charAt(index) returns the character in stringObject that gets the index position

stringObject.charCodeAt(index) returns the character encoding of the character that gets the index position in stringObject.

2. Want to get the position of a character in a string (as used in an array)

stringObject.indexof("the string you want"); returns - 1 if the position of the return string is not present

stringObject.lastindexof("the string you want"); search backwards and forwards to return the position of the string

 1 <script type="text/javascript">
 2 var str="hello world";
 3    //1.Want to get a character in a string:
 4    console.log(str.charAt(7));//Display the position character of index 7;
 5    console.log(str.charCodeAt(4));//Displays characters indexed in 4 bits in ASCII Code number on code table
 6    //2.Want to get the position of a character in a string:
 7    var email="";
 8    console.log(email.indexOf("o"));//Find Strings o The index value of the first occurrence location;
 9    console.log(email.lastIndexOf("."));//Find Strings"."Finally, the index value of the location where it is located appears once.
10    </script>

3-8 String (slice()-substring() and substr())


1 want to intercept string

1.slice(start,end) interception

1. When the last parameter is omitted, it is intercepted to the end.

2. When the parameter is negative, the value is the string length + the negative number.

3, excluding the last parameter itself (end).

1 <script type="text/javascript">
2     var str="hello world";
3     //Intercept orl
4     console.log(str.slice(7,10));
5     console.log(str.slice(1));
6     console.log(str.slice(-7,-2));//str.slice(4,9)
7     </script>

2 substring() interception

Note: The grammar and function are exactly the same as slice().

The difference is that:

1. When the parameter is negative, the parameter is automatically converted to 0.

2.substring() takes the smaller number as the starting position and the larger number as the ending position.

1 <script type="text/javascript">
2     var str="hello world";
3     console.log(str.substring(-7,5));//(0,5)
4     console.log(str.substring(2,-5));//(0,2)
5 </script>

3.substr() interception;




Intercept substrings.

Description of parameters:

1.start: It is necessary to specify the starting position of the substring.

2.len: Optional, representing the total number of characters intercepted, when omitted, to the end of the string.

3. When start is negative, it adds the incoming negative value to the length of the string.

4. When len is negative, it returns an empty string.

1   <script>
2          var str="hello world";
3     console.log(str.substr(6,3));    //Equivalent to str.substring(6,9)
4     console.log(str.substr(-5,4));//(6,4)
5     console.log(str.substr(3,-4));// If the number of empty intercepts is negative or zero, the empty string is returned.
6     </script>  

Method synthesis for obtaining a specific segment in a string

 1 <script type="text/javascript">
 2     //Get the extension
 3     var url=""
 4     function getFileFormat(url){
 5         //Obtain.stay URL The position that appears in it
 6         var pos=url.lastIndexOf(".");
 7         return url.substr(pos);
 8     }
 9     var formatName=getFileFormat(url);
10     var picFormat=getFileFormat("123456789.jpg");
11     console.log(formatName);
12     console.log(picFormat);
13     </script>

3-20 String (split and replace)




Divide a string into an array of strings to return a value: Array specifies: necessary, delimiter.

1 <script type="text/javascript">
2        var str="welcome-to-beijing";
3        //Use split take str Convert to an array
4        var arr=str.split("-");
5        console.log(arr);//["welcome","to","beijing"]
6        var date="2016/05/05";
7        var dateArr=date.split("");
8        console.log(dateArr);//["2016","05","05"]
9    </script>

2 replace()

stringObj.replace(regexp/substr, replacement)

Replace some characters with others in a string, or a substring that matches a regular expression.

Return value: string

Parameter: regexp: Required. RegExp objects that specify substrings or patterns to be replaced. replacement: Required, a string value.

1 <script>
2  //replace replace
3      var tel="010-62971268,010-64899373,010-34565767";
4      //newTel Replaced string
5      var newTel=tel.replace(','," ");
6      console.log(newTel);
7 </script>

3-26 String (to UpperCase and to Lower Case)


Other ways to master string methods:


Convert strings to uppercase


Converting strings to lowercase

3. Convert the string border-left-color to borderLeftColor

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8     <script type="text/javascript">
 9         function cameback(str){
10             //adopt-This style symbol will str Split into arrays
11             var arr=str.split("-"),newStr=arr[0];
12             //console.log(arr);//["border","left","color"]
13             //Words in position 0 need not be processed, so I=1 start
14             for(var i=1,len=arr.length;i<len;i++){
15                 var world=arr[i];
16                 //Convert the initials of each word into capitals
17                 newStr+=world.charAt(0).toUpperCase()+world.substr(1);
18                 //console.log(newStr);//Left Color  L  C
19                 //newStr=borderLeftColor
20             }
21             return newStr;
22         }
23         var comelFormat=cameback("border-left-color");
24         console.log(comelFormat);
25     </script>
26 </body>
27 </html>



Tags: Javascript encoding ascii

Posted on Wed, 09 Oct 2019 05:26:10 -0700 by C_Calav