JavaScript: Use & nbsp; JavaScript & nbsp; to operate string functions. Javascript tutorial
Although JavaScript is useful, processing strings is one of the most popular. The following describes how to use JavaScript to operate strings. In JavaScript, String is an object. String objects are not stored as character arrays, so we must use built-in functions to manipulate their values. These built-in functions provide different methods to access the content of string variables. Let's take a closer look at these functions.
All-encompassing
The value of the operation string is common for developers. There are many methods to operate on a string, such as extracting part of the content from a string or determining whether a string contains a specific character. The following JavaScript Functions provide developers with all the functions they need:
• Concat ()? Combines two or more characters to return a new string.
• IndexOf ()? Returns the index at the first part of a substring in a string. If no match exists,-1 is returned.
• CharAT ()? Returns the characters at the specified position.
• LastIndexOf ()? Returns the index of the last occurrence of a substring in a string. If no match exists,-1 is returned.
• Match ()? Check whether a string matches a regular expression.
• Substring ()? Returns a substring of a string. The input parameters are the start position and end position.
• Replace ()? It is used to find a string that matches a regular expression, and then replaces the matched string with a new string.
• Search ()? Execute a regular expression to match and search. If the search is successful, the matching index value in the string is returned. Otherwise,-1 is returned.
• Slice ()? Extract A part of the string and return a new string.
• Split ()? Divides a string into substrings to form a string array.
• Length ()? Returns the length of a string. The length of a string refers to the number of characters contained in the string.
• ToLowerCase ()? Converts the entire string to lowercase letters.
• ToUpperCase ()? Converts the entire string into uppercase letters.
Note: The concat, match, replace, and search functions are added to JavaScript 1.2. All other functions are provided in JavaScript 1.0.
Next let's take a look at how to use these functions in JavaScript. The following code uses all the functions mentioned above:
1 function manipulateString(passedString1, passedString2) { 2 3 var concatString; 4 5 // The string passed to concat is added to the end of the first string 6 7 concatString = passedString1.concat(passedString2); 8 9 alert(concatString); 10 11 // The following if statement will be true since first word is Tony 12 13 if (concatString.charAt(3) == "y") { 14 15 alert("Character found!"); 16 17 } 18 19 // The last position of the letter n is 10 20 21 alert("The last index of n is: " + concatString.lastIndexOf("n")); 22 23 // A regular expression is used to locate and replace the substring 24 25 var newString = concatString.replace(/Tony/gi,"General"); 26 27 // The following yields Please salute General Patton 28 29 alert("Please salute " + newString); 30 31 // The match function returns an array containing all matches found 32 33 matchArray = concatString.match(/Tony/gi); 34 35 for (var i=0; i<matchArray.length;i++) { 36 37 alert("Match found: " + matchArray[i]); 38 39 } 40 41 // Determine if the regular expression is found, a ?1 indicates no 42 43 if (newString.search(/Tony/) == -1) { 44 45 alert("String not found"); 46 47 } else { 48 49 alert("String found."); 50 51 } 52 53 // Extract a portion of the string and store it in a new variable 54 55 var sliceString = newString.slice(newString.indexOf("l")+2,newString.length); 56 57 alert(sliceString); 58 59 // The split function creates a new array containing each value separated by a space 60 61 stringArray = concatString.split(" "); 62 63 for (var i=0; i<stringArray.length;i++) { 64 65 alert(stringArray[i]; 66 67 } 68 69 alert(newString.toUpperCase()); 70 71 alert(newString.toLowerCase()); 72 73 }
[1] [2] Next page