Home  >  Article  >  Web Front-end  >  Learn the basics of JavaScript string objects in one article

Learn the basics of JavaScript string objects in one article

WBOY
WBOYforward
2022-06-21 11:43:241825browse

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.

Learn the basics of JavaScript string objects in one article

[Related recommendations: javascript video tutorial, web front-end

Basic concepts

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

  • Immutability of strings

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.

Return the position according to the character

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.

1, indexOf('Character to be found', [starting position])

Search from front to back

var str="你是我的神";
console.log(str.indexOf('我');//2
console.log(str.indexOf('我',1);

2, lastIndexOf('Character to be found' ,[starting position])

Search from back to front

var str="你是我的神";
console.log(str.lastIndexOf('我');

Return characters according to position

1, charAt (index number)

Return the specified The character at the position

var str="andy";
console.log(str.charAt(3));

2. Traverse all characters

for(var i=0;i<str.length;i++){
 console.log(str.charAt(i));
}

3. charCodeAt (index number)

returns the ASCII code, which can be used to determine which key the user pressed.

console.log(charCodeAt(o));

4. str[index]

H5 is new and has compatibility issues

String operation method

1.concat('string 1','String 2',…)

var str=&#39;cindy&#39;;
console.log(str.concat(&#39;red&#39;);

2. substr(start,length)

Take length characters starting from start

var str=&#39;你是我的神&#39;;
console.log(str.substr(2,2));

3.Replace Character replace('replaced character','replaced character')

Replace only the first character

var str=&#39;andandand&#39;;
console.log(str.replace(&#39;a&#39;,&#39;b&#39;);//bndandand

4. Characters are converted into array split('separator')

var str=&#39;red,pink,blue&#39;;

console.log(str.split(&#39;&&#39;));

5. Convert toUpperCase() to uppercase

6. Convert toLowerCase() to lowercase


Simple case

  • Find the position and number of occurrences of all g in 'abcdefgabcdefg'

  var str=&#39;abcdefgabcdefg&#39;;
        var index=str.indexOf(&#39;g&#39;);
        var num=0;
        while(index!==-1){
            console.log(index);
            num++;
            index=str.indexOf(&#39;g&#39;,index+1);
        }
        console.log(&#39;g出现的次数是:&#39;+num);
  • Determine the most frequent characters in a string 'abcdefgabcdefg' and count the number of times

var str=&#39;abcdefgabcdefg&#39;;
       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=&#39;&#39;;
       for(var k in obj){
           if(obj[k]>max){
               max=obj[k];
               ch=k;
           }
       }
       console.log(max);
       console.log('最多的字符为'+ch);
  • 'abcoefoxyozzoopp'Replace o with *

 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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete