Basic packaging types: wrap simple data types into complex data types. (Basic data types have properties and methods)
var str='andy'; console.log(str.length);
- wrap simple data types into complex data types
var temp=new String('andy');
- give the value of the temporary variable to str
str=temp;
- destroy the temporary variable
temp=null;
JavaScript provides Three special reference types: string, number, boolean.
Do not splice a large number of strings, because the original strings are also in memory. The value inside does not change. Although it seems that the content can be changed, in fact, the address has changed and a new memory space has been opened in the memory.
All methodswill not modify the string itself (strings are immutable), and a new string will be returned when the operation is completed.
searches from front to back
var str="You are my God"; console.log(str.indexOf('I');//2 console.log(str.indexOf('I',1);
Search from back to front
var str="You are my God"; console.log(str.lastIndexOf('I');
returns the character at the specified position
var str="andy"; console.log(str.charAt(3));
for(var i=0;i<str.length;i++){ console.log(str.charAt(i)); }
returns the ASCII code, which can be used to determine which key the user pressed.
console.log(charCodeAt(o));
is new in H5 and has compatibility issues.
var str=' cindy'; console.log(str.concat('red');
takes the length of characters starting from start
var str='You are my God'; console.log(str.substr(2,2));
only replaces the first character
var str='andandand'; console.log(str.replace('a','b');//bndandand
var str='red,pink,blue'; console.log(str.split('&'));
var str='abcdefgabcdefg'; var index=str.indexOf('g'); varnum=0; while(index!==-1){ console.log(index); num++; index=str.indexOf('g',index+1); } console.log('The number of occurrences of g is:'+num);
var str='abcdefgabcdefg'; var obj={}; for(var i=0;i<str.length;i++){ var chars=str.charAt(i); if(obj[chars]){ obj[chars]++; }else{ obj[chars]=1; } } console.log(obj); var max=0; var ch=''; for(var k in obj){ if(obj[k]>max){ max=obj[k]; ch=k; } } console.log(max); console.log('The most characters are '+ch);
var str='abcoefoxyozzoopp'; while(str.indexOf('o')!==-1){ str=str.replace('o','*'); } console.log(str);