Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String 的一些常用方法 #10

Open
GRPdream opened this issue Apr 19, 2019 · 0 comments
Open

String 的一些常用方法 #10

GRPdream opened this issue Apr 19, 2019 · 0 comments

Comments

@GRPdream
Copy link
Owner

方法列表

描述
String.charAt(index) 返回给定位置(index)的字符
String.charCodeAt(index) 返回给定位置(index)字符的字符编码
String.concat(value) 将一或多个字符串拼接起来,返回拼接得到的新的字符串
String.slice(value1, value2) 返回被操作字符串的一个子字符串
String.indexOf(value) 从一个字符串搜索指定的子字符串,返回子字符串的位置(没有找到返回-1)
String.match(正则) 字符串的模式匹配方法
String.search( 正则) 返回字符串中第一个匹配项的索引
String.replace(被替换的值, 用于替换的值) 替换指定部分的字符串

1.String.charAt(index)

    var stringValue = "hello world";  
    alert(stringValue.charAt(1)); //"e" 

2.String.charCodeAt(index)

    var stringValue = "hello world";  
    alert(stringValue.charCodeAt(1)); //输出"101" 

3.String.concat(value)

    var stringValue = "hello ";  
    var resrult = stringValue.concat("world");  
    alert(resrult); //"hello world"  
    alert(stringValue); //"hello " 

4.String.slice(value1, value2)

     var stringValue = "hello world";  
     alert(stringValue.slice(3)); //"lo world" 
     alert(stringValue.slice(3,7)); //"lo w"   

5.String.indexOf(value)

     var stringValue = "hello world";  
     alert(stringValue.indexOf("o"));     //4  
     alert(stringValue.lastIndexOf("o")); //7  

6.String.trim( )

     var stringValue = "  hello world  ";  
     var trimSting = stringValue.trim();  
     alert(trimSting); //"hello world"  
     alert(stringValue); //"  hello world  "

7.String.match(正则)

     var text = "cat,bat,sat,fat";  
     var pattern = /.at/;  
     var matches = text.match(pattern);  
     alert(matches[0]); //"cat"  

8.String.search(value)

     var text = "cat,bat,sat,fat";  
     var pos = text.search(/at/);  
     alert(pos); //1  

9.String.replace(被替换的值, 用于替换的值)

     var text = "cat,bat,sat,fat";  
     var result = text.replace("at","ond");  
     alert(result); //"cond,bat,sat,fat"  
     result = text.replace(/at/g,"ond");  
     alert(result); //"cond,bond,sond,fond"  

参考文章

JS字符串的一些常用方法

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant