<script type= "Text/javascript" >varStr= "ABCDEFBC? One by one, 123 ff,6 ";varStr2=NewString ("Ghl"); Console.log ("typeof:" +typeofSTR);//typeof StringConsole.log ("typeof:" +typeofSTR2);//typeof Object wrapper class//first, get the classConsole.log ("CharAt:" +str.charat (5));//returns the character at the specified position, with the subscript starting at 0 FConsole.log ("charCodeAt:" +str.charcodeat (0))//returns the encoding of the specified characterConsole.log ("fromCharCode:" +string.fromcharcode (97))//encoding converted to character a//second, find the classConsole.log ("IndexOf:" +str.indexof ("CBA"));//returns the index (from left to right) of the first occurrence of a substring in a string. If there is no match, return-1; -1Console.log ("IndexOf:" +str.indexof ("C"));//2 Returns the matching index value in a stringConsole.log ("LastIndexOf:" +str.lastindexof ("C"));//returns the index (right-to-left search) of a substring "last place" in a string, or 1 if there is no match, returns the actual index. 7Console.log ("Search:" +str.search ("BC"));//performs a regular expression matching lookup. If the lookup succeeds, returns the matching index value in the string. otherwise returns-1. 1/*the difference between indexof and search: Search can match regular expressions, search ("?"), search can not find? + Equal quantifiers, if required only with regular: Search (/\?/)*/Console.log ("Search:" +str.search (/\?/));//8Console.log ("Match:" +str.match (/\d+/g));//11,123,6//match checks a string to match a regular expression content, if no match returns NULL. Depends on the regular, otherwise it doesn't make sense./*Replace is used to find a string that matches a regular expression, and then replaces the matching string with the new string. Returns a new string that was obtained after the first match or all matches of regexp were replaced with replacement. Stringobject.replace (regexp/substr,replacement)*/Console.log ("Replace:" +str.replace ("BC", "BC"));//ABCDEFBC, 123 ff,6Console.log ("Replace:" +str.replace (/bc/, "BC"));//ABCDEFBC, 123 ff,6Console.log ("Replace:" +str.replace (/bc/g, "BC"));//ABCDEFBC, 123 ff,6;g represents a global lookup//third, interception class/*The Slice () method extracts a portion of a string and returns the extracted part with a new string. The Stringobject.slice (start,end) parameter describes the starting subscript for the fragment to be extracted by start. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on. End subscript at the end of the fragment to be extracted immediately. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string. */Console.log ("Slice:" +str.slice (2));//CDEFBC, 123 ff,6Console.log ("Slice:" +str.slice (-2));//, 6Console.log ("Slice:" +str.slice (1,6));//, Cdef/*the substring () method is used to extract the character of a string intermediary between two specified subscripts. Stringobject.substring (Start,stop) differs from the slice () and substr () methods, substring () does not accept negative parameters slice () is more flexible than substring (), Because it allows the use of negative numbers as arguments. */varstr3= "Hello world!"Console.log ("SUBSTRING:" +str3.substring (3,7))//the number between lo W 3-7/*The substr () method extracts a specified number of characters from the start subscript in a string. STRINGOBJECT.SUBSTR (start,length) Important: ECMAscript did not standardize the method and therefore objected to its use. *///toLowerCase, touppercase--uppercase and lowercase conversionsConsole.log ("toLowerCase:" +str3.tolowercase ())//Hello world! lowercaseConsole.log ("toUpperCase:" +str3.touppercase ())//HELLO world! UppercaseConsole.log ("Length:" +str3.length)//returns 12, counting starting from 1/*The concat () method is used to concatenate two or more strings. Grammar Stringobject.concat (stringx,stringx,..., stringx)*/Console.log ("Concat:" +str3.concat (str))//Hello WORLD!ABCDEFBC, 123 ff,6Console.log ("+:" +str3+str)//Hello WORLD!ABCDEFBC, 123 ff,6varabc= "Hello World"varAbc2=abc.split (""); Console.log ("+:" +abc.split (""). Join (""))//h e l l o w o R l DConsole.log ("+:" +typeofABC2)//h e l l o w o R l DConsole.log ("+:" +abc2.push ("F"))//h e l l o w o R l DConsole.log ("+:" +ABC2)//h e l l o w o R l D/*the Split () method is used to split a string into an array of strings. After splitting an array, you can use the arrays-related usage method to reverse a string array stringobject.split (Separator,howmany) into a string array. The array is created by splitting the string stringobject into substrings at the boundary specified by separator. The string in the returned array does not include the separator itself. Separator required. A string or regular expression that splits stringobject from where specified by the parameter. Howmany is optional. This parameter specifies the maximum length of the returned array. If this parameter is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length. Hints and Comment notes: If you use an empty string ("") as a separator, then each character in the Stringobject will be split. Note: The action performed by String.Split () is the opposite of what Array.join does. */varStr4= "How is you doing today?"Console.log ("Length:" +str4.length)// -Console.log ("Split:" +str4.split (""))//H,o,w,, A,r,e,, Y,o,u,, D,o,i,n,g,, T,o,d,a,y,?Console.log ("Length:" +str4.length)// -Console.log ("Split:" +str4.split (""))//how,are,you,doing,today?Console.log ("Split:" +str4.split ("", 3))//how,are,youConsole.log ("Split:" +str4.split ("|", 3))//How is you doing today? No | number, return initial valueConsole.log ("2:3:4:5". Split (":"))//will return ["2", "3", "4", "5"]Console.log ("2?3?4?5". Split ("?"))//will return ["2", "3", "4", "5"]Console.log ("Hello". Split (""))//["H", "E", "L", "L", "O"]</script>
JavaScript string notes