Home > Article > Web Front-end > Learn the basics of JavaScript string objects in one article
This article brings you relevant knowledge about javascript, which mainly introduces related issues about string objects, including some basic concepts and practical uses, etc. Let’s talk about it together Take a look, hope it helps everyone.
[Related recommendations: javascript video tutorial, web front-end】
Basic packaging type: wrap simple data types into complex data types. (Basic data types have properties and methods)
var str='andy'; console.log(str.length);
- Package simple data types into complex data types
var temp=new String('andy');
- Put temporary variables The value is given to str
str=temp;
- Destroy the temporary variable
temp=null;
JavaScript provides three special reference types: string, number, boolean
Don’t 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 methods of the string will not modify the string itself (the string is immutable), and a new string will be returned after the operation is completed.
Search from front to back
var str="你是我的神"; console.log(str.indexOf('我');//2 console.log(str.indexOf('我',1);
Search from back to front
var str="你是我的神"; console.log(str.lastIndexOf('我');
Return the specified The character at the 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));
H5 is new and has compatibility issues
var str='cindy'; console.log(str.concat('red');
Take length characters starting from start
var str='你是我的神'; console.log(str.substr(2,2));
Replace only 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'); var num=0; while(index!==-1){ console.log(index); num++; index=str.indexOf('g',index+1); } console.log('g出现的次数是:'+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('最多的字符为'+ch);
var str='abcoefoxyozzoopp'; while(str.indexOf('o')!==-1){ str=str.replace('o','*'); } console.log(str);
[Related recommendations: javascript video tutorial, web front end】
The above is the detailed content of Learn the basics of JavaScript string objects in one article. For more information, please follow other related articles on the PHP Chinese website!