Built in object - String object

1, Create a string object (string)

Gets the length of the string   ( Length method)

<script type="text/javascript">
			// Create string object
			var str =  'i'am a student';				//Relatively simple
			console.log(str);
			var str1 = new String('i'am a teacher')		//More complex
			console.log(str1);
            var length = str.length;
			console.log(length);                         //14
			var length1 = str1.length;
			console.log(length1);                        //14
		</script>

2, Case conversion method

1.string.toLowerCase(): convert string to lowercase

2.string.toUpperCase(): convert string to uppercase

<script type="text/javascript">
			// Create string object
			var a = 'abcdefg';
			var b = 'QWERTYYUI';
			var str = a.toUpperCase()
			console.log(str);					// ABCDEFG
			var str1 = b.toLowerCase()
			console.log(str1);					// qwertyyui
		</script>

3, Extract a character

1.string.charAt(index): returns the character of the specified subscript in the string

2.string.charCodeAt(index): returns the unicode encoding of the character with the specified subscript

<script type="text/javascript">
			// Create string object
			var a = 'qwer I know the glory. I've been away for a long time';
			var b = a.charAt(2);
			console.log(b);
			var c = a.charCodeAt(2)
			console.log('e of ASCII Code is:' + c);			// The ASCII code of e is 101
			var d = a.charCodeAt(5);
			console.log(d);								// 30693
					
		</script>

4, Find the specified string

1.string.indexof(findstr,index) returns the position where the specified text first appears in the string

2.string.lastindexof(findstr,index)   Returns the last occurrence of the specified text in a string

3.string.search() searches for a specific string and returns the matching location

<script type="text/javascript">
			// indexof returns the index of the first occurrence of the string
			var str = 'what is the matter with you i anyway';
			var a = str.indexOf('i')	 // 5
			console.log(a);
			
			/* If indexof is followed by a value, the printed result depends on the number,
			If greater than the first and less than the second, the subscript of the second value is printed
			Greater than the second and less than the third, the subscript position of the third is printed */
			
			console.log(str.indexOf('i',6));  		// 20
			console.log(str.indexOf('i',21));  		// 28
			
			// lastindexof returns the subscript of the last occurrence of the string	
			var b = str.lastIndexOf('i');
			console.log(b);				// 28
			
			/* If lastindexof is followed by a value, the printed result depends on the number,
			If the value is x, the characters from subscript 0 to x will be found,
			And print out the largest one. If it is not found, return - 1*/
			
			console.log(str.lastIndexOf('i',6));  		// 5
			console.log(str.lastIndexOf('i',21));  		// 20
			
			//If the character is not found, - 1 is returned
			var c = str.indexOf('b')
			console.log(c);

			// search searches for a specific string. The second parameter has no effect
			var e = str.search('i')
			console.log(e);							// 5
			console.log(str.search('i',21));		// 5
		</script>

What is the difference between search() and indexOf()

1. The parameter of search () must be a regular expression, while the parameter of indexOf() is just an ordinary string
indexOf() is a lower level method than search()
2. If only a specific string is searched, the system using indexOf() consumes less resources and is more efficient
If you look up a string with certain characteristics (for example, looking up a string starting with a followed by a number), indexOf() can't do anything. You have to use regular expressions and search() methods
3. Most of the time, using indexOf() does not really want to know the position of the substring, but whether the long string contains the substring. If the returned index is - 1, it means no, otherwise it means yes

4, Extract partial string

1.slice() method: slice(start, end)

2.substring() method: substring(start, end)

3.substr() method: substr(start, length)

<script type="text/javascript">
			/* slice()method */
			/* Extract all strings from subscripts 5 to 10 */
			var str = 'what is the matter with you anyway';
			var str1 = str.slice(5, 11)   /* Excluding 10*/
			console.log(str1);
			/* Extract all strings after subscript 7 */
			var str2 = str.slice(7)
			console.log(str2);
			/* Extract all strings with subscripts - 11 to - 7 from the back */
			var str3 = str.slice(-11,-7)
			console.log(str3);
			/* Extract all strings after subscript - 11 */
			var str4 = str.slice(-11)
			console.log(str4);
			
			/* substring()method */
			var res = str.substring(2,11)
			console.log(res);
			var res1 = str.substring(7)
			console.log(res1);
			
			/* Cannot accept negative index, syntax correct, no value */
			var res2 = str.substring(-13,-7)
			console.log(res2);
			var res3 = str.substring(-7)
			console.log(res3); /* Print it all out*/
			
			/* substr()method */
			/* The second parameter specifies the string length of the extracted part. The second parameter cannot be negative because it refers to length. */
			var sub = str.substr(2,10)
			console.log(sub);
			var sub1 = str.substr(7)
			console.log(sub1);
			var sub2 = str.substr(-13,7)
			console.log(sub2);
			var sub3 = str.substr(-13)
			console.log(sub3);
		</script>

5, Operation string

1.string.trim():   Delete white space characters at both ends of the string

2.string.concat():   Connect two or more strings

3.string.split(): connect strings according to the characters in brackets

4.string.replace(): replace the value specified in the string with another value

<script type="text/javascript">
			// string.trim() is not very useful for removing spaces
			var str = ' what is ';
			var str1 = 'happened';
			var a = str.trim()
			console.log(a);

			// string.concat() connects two strings
			var b = str.concat(str1)
			console.log(b);					 // what is happened
			
			// string.split() splits a string in parentheses. It gives a null value, so it returns letters
			var c = str1.split('');
			console.log(c);					 //["h", "a", "p", "p", "e", "n", "e", "d"]
			
			// string.replace() replaces the string
			var str2 = 'aabbccdd';
			var d = str2.replace(/a/g, "z")   // replace all
			console.log(d);
			var e = str2.replace(/a/i, "z")		//First replacement
			console.log(e);
		</script>

6, Case:

There is an array var array= ["America","Greece","Britain","Canada","China","Egypt"]

Counts the number of strings containing "a" in the array

<script type="text/javascript">
			/* There is an array var array= ["America","Greece","Britain","Canada","China","Egypt"]
			Counts the number of strings containing "a" in the array */
            //  First convert the array into a string, then split it into letters, traverse and judge
			var array= ["America","Greece","Britain","Canada","China","Egypt"]
			var str = array.toString();
			var str1 = str.split('');
			var j = 0;
			console.log(str1);
			for(var i = 0;i < str1.length;i++){
				if(str1[i] == 'a'){
					j++
				}
			}
			console.log(j);
		</script>

Tags: Javascript html

Posted on Mon, 06 Sep 2021 21:32:24 -0400 by billy_111