1. The index of the string starts from 0, which means that the first character index value is [0], the second is [1], and so on.
You can use quotation marks in strings, the quotation marks in the string should not be the same as the quotation marks in the string
2. String length
//declare a constant var txt = "Hello World!"; //Determine the length of a string document.write( txt.length );
3. Methods of string built-in objects
charAt() | Returns the character at the specified index position |
charCodeAt() | Returns the Unicode value of the character at the specified index position |
concat() | Concatenates two or more strings and returns the concatenated string |
fromCharCode() | Convert Unicode to String |
indexOf() | Returns the position in the string that retrieves the first occurrence of the specified character |
lastIndexOf() | Returns the position in the string that retrieves the last occurrence of the specified character |
localeCompare() | Compare two strings in a local specific order |
match() | Find a match for one or more regular expressions |
replace() | Replace substrings that match a regular expression |
search() | Retrieve values that match a regular expression |
slice() | Extract a fragment of a string and return the extracted part in a new string |
split() | Split a string into an array of substrings |
substr() | Extract the specified number of characters from the string from the starting index number |
substring() | Extracts characters between two specified index numbers in a string |
toLocaleLowerCase() | Converts strings to lowercase according to the host's locale, only a few languages (eg Turkish) have locale-specific case mappings |
toLocaleUpperCase() | Converts strings to uppercase according to the host's locale, only a few languages (such as Turkish) have local-specific sizes |
4. Writing method of three attributes
The first: use object
//The first //custom object, use object var student=new Object() //Set some properties on the object student.stuID="1001" student.stuName="Zhang San" student.calssName="MOBILE 2103 CLASS" //set function to object student.sayhellow=function(){ console.log("big plus sign") } student.sayhellow()
Second: Constructor
//Second: Constructor function teacher(stuid,stuname){ //this in the constructor represents the current object this.stuid=tid this.stuname=tname this.Eat=function(){ console.log("Have a meal") } }
The third: other methods
//The third method: other methods var stu={ stuid:"1002", stuname:"Li Si", study:function(){ console.log("study") } } stu.study() console.log(stu.stuid)
5. Example 1:
first number:<input type="text" id="one" /><br /> second number:<input type="text" id="two" /><br /> Operation method:<input type="button" name="" id="jiafa" value="+" onclick="cal_1('+')"/> <input type="button" name="" id="jianfa" value="-" onclick="cal_1('-')"/> <input type="button" name="" id="chengfa" value="*" onclick="cal_1('*')"/> <input type="button" name="" id="chufa" value="/" onclick="cal_1('/')"/><br /> operator:<input type="text" name="result" id="result" value="" /> <script> function cal_1(y){ //Get the value entered in the textbox var one=document.getElementById("one").value var two=document.getElementById("two").value var result if(y=="+"){ result=parseFloat(one)+parseFloat(two) }else if(y=="-"){ result=parseFloat(one)-parseFloat(two) }else if(y=="*"){ result=parseFloat(one)*parseFloat(two) }else{ result=parseFloat(one)/parseFloat(two) } document.getElementById("result").value=result } </script>
Case 2:
first number:<input type="text" id="one" value="" />+ second number:<input type="text" id="two" value="" />= <input type="text" id="result" value="" /> <input type="button" name="" id="" value="operation" onclick="cal()"/> <script type="text/javascript"> function cal() { var one=document.getElementById("one").value var two=document.getElementById("two").value var result=parseFloat(one)+parseFloat(two) document.getElementById("result").value=result } </script>