Home > Web Front-end > JS Tutorial > body text

Learn the basics of JavaScript string objects in one article

WBOY
Release: 2022-06-21 11:43:24
forward
1729 people have browsed it

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);
Copy after login
  • Package simple data types into complex data types
var temp=new String('andy');
Copy after login
  • Put temporary variables The value is given to str
str=temp;
Copy after login
  • Destroy the temporary variable
temp=null;
Copy after login

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);
Copy after login

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

Search from back to front

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

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));
Copy after login

2. Traverse all characters

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

3. charCodeAt (index number)

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

console.log(charCodeAt(o));
Copy after login

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;);
Copy after login

2. substr(start,length)

Take length characters starting from start

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

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
Copy after login

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

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

console.log(str.split(&#39;&&#39;));
Copy after login

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);
Copy after login
  • 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);
Copy after login
  • 'abcoefoxyozzoopp'Replace o with *

 var str='abcoefoxyozzoopp';
        while(str.indexOf('o')!==-1){
            str=str.replace('o','*');
        }
        console.log(str);
Copy after login

[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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!