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 character at the specified position.
• Lastindexof ()-returns the index that appears at the end 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 ()-used to find a string that matches a regular expression, and then use a new string to replace the matched string.
• Search ()-execute a regular expression to match the search results. 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 so-called length of a string refers to the number of characters it contains.
• Tolowercase ()-converts the entire string into 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.
Remove leading and trailing spaces: Str. Replace (/(^ \ s *) | (\ s * $)/g ,"")