涉及字符串时,常用到的几个方法...

----------------------------------------------------------------------------------------------------

1.   charAt() 方法可返回指定位置的字符。

var str="Hello world!";

//charAt()
console.log(str.charAt(0)); //H

2.   concat() 方法用于连接两个或多个字符串。

var str1="Hello ";
var str2="world!";

console.log(str1.concat(str2));

3.  indexOf()  方法返回 某个指定的字符串值 在 字符串中 首次 出现的位置。

注意: 如果没找到,则返回 -1, 对大小敏感

var str="Hello world!";

console.log( str.indexOf("H") );
//0
console.log( str.indexOf("Hello") );
//0
console.log( str.indexOf("world") );
//6
console.log( str.indexOf("h") );
-1

4.  lastIndexOf()  方法返回 返回一个 指定的字符串值 最后 出现的位置, 从后向前查找。

var str="Hello world!";

console.log( str.lastIndexOf("e") );
//0
console.log( str.lastIndexOf("Hello") );
//0
console.log( str.lastIndexOf("world") );
//6
console.log( str.lastIndexOf("h") );
//-1
console.log( str.lastIndexOf("!") );
//11

5. match()  方法  在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。

注意: 此方法返回的是 指定的值(要查找的值),而不是字符串的位置。

var str="Hello world!"
console.log( str.match("world") );
//world
console.log( str.match("World") );
//null
console.log( str.match("world") );
//world
console.log( str.match("worlld") );
//null
console.log( str.match("world!") );
//world!
console.log( str.match("o") );
//o

6. replace()  方法 替换字符, 或替换一个与正则表达式匹配的字符串。

var str="Hello world!"
console.log( str.replace("Hello", "Wonderful") );
//Wonderful world!

7. serach()  方法 查找字符串中的字符 (返回的是 字符在当前字符串中的第一次出现的位置), 或检索与正则表达式相匹配的子字符串。

var str="Hello world!"
console.log( str.search("Hello") );
//0
console.log( str.search("o") );
//4

8.  slice()  方法 截取字符串, 返回值是 截取出来的新字符串。

注意: 规则是 左闭右开(开始位置包括, 结束位置不包括)

var str="Hello happy world!"

console.log( str.slice(1, 3) );
//el
console.log( str.slice(0, 5) );
//Hello
console.log( str.slice(0) );
//Hello happy world!

console.log( str.slice( ) );
//Hello happy world!

9.  split()  方法 分割字符串 / 把字符串分割成字符串数组。

注意:

  1. 如果引号里有空格的话, 那该方法 就以空格为标准,来分割字符串

  2. 如果引号里什么也没有的话,那改方法就会把字符串分割成每个字符

  3. 该方法有两个参数, para2 表示保留分割的位数

  4. 改方法的返回类型是 object类型的

var str="How are you!"

console.log( str.split(" ") );
//["How", "are", "you!"]
console.log( str.split("") );
//["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", "!"]
console.log( str.split() );
//["How are you!"]
console.log( str.split(" ", 3) );

10. substring()  方法 截取字符串(起始位置 与 结束位置之间的)。

注意:

  1. 改方法有两个参数 起始位置, 结束位置 (左闭右开)

  2. 返回类型是String类型的

var str="Hello world!";

console.log(str.substring(1));
//ello world!
console.log(str.substring(1, 5));
//ello
console.log(str.substring());
//Hello world!

11.  toString()  改方法返回字符串( 转成字符串 )。

12. 把字符串转成大、小写  

   toUpperCase(), toLocaleUpperCase() 把字符串转成大写

   toLowerCase(), toLocaleLowerCase() 把字符串转成大写

Javascript ----字符串(String)中的方法的更多相关文章

  1. JavaScript 字符串(String)对象的方法

    anchor() 描述:用于创建 HTML 锚 原型:stringObject.anchor(anchorname) 用法: <script> var txt="Hello wo ...

  2. JS json对象(Object)和字符串(String)互转方法

    [JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...

  3. JavaScript字符串String

    JavaScript中String类型用于表示由零个或者多个16位Unicode字符组成的字符序列即字符串:同时字符串可以用单引号或双引号表示. 下面是一些特殊的字面量: 字面量 含义\n 换行\t ...

  4. JavaScript 字符串(String)对象

    String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); String(s); 参数 参数 s 是要存储在 String 对象中或 ...

  5. JavaScript 字符串(String) 大全

    JavaScript字符串存储一系列字符,如“John Doe”.字符串可以是双引号或单引号内的任何文本: <!DOCTYPE html> <html> <meta ch ...

  6. java String 中 intern方法的概念

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  7. String中concat方法小记

    介绍String中的concat方法使用: 日常开发中,经常对字符串进行处理,之前碰到多个字符串拼接,要么使用stringBuilder,要么使用StringBuffer,再或者是直接多个String ...

  8. String中intern方法的作用

    前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池 ...

  9. C++中 string 中的方法的使用详解

    string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法 1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截 ...

随机推荐

  1. iOS 沙盒(sandbox)机制和文件操作

    本文参看了 http://www.uml.org.cn/mobiledev/201209211.asp#1 这篇文章中的介绍,尊重原著. 1.IOS沙盒机制 IOS应用程序只能在本应用程序中创建的文件 ...

  2. OA系统部门结构树

    public class DepartmentUtils { /** * @param topList 顶级部门列表 * @param removeId 删除部门的id * @return */ pu ...

  3. css三级下拉的导航栏

    #menu{ height: 65px; width:100%; background-color: rgba(0, 0, 0, 0.5);}#menu ul{ list-style: none;}# ...

  4. rand()和srand()GetTickCount函数用法

    标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数: 函数一:int rand(void):从srand (seed)中指定的seed开始 ...

  5. JUnit org.junit.runner.Request.classWithoutSuiteMethod解决方法

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  6. linux笔记:文件处理命令touch,cat,more,less,head,tail

    命令名称:touch功能:新建文件命令所在目录:/bin/touch用法:touch 文件名 命令名称:cat功能:显示文件内容命令所在目录:/bin/cat用法:cat [-n] 文件名参数:-n ...

  7. xml中的xmlns,xmlns:xsi,xsi:schemaLocation有什么作用,如果没有会怎么样呢

    如 maven 的 pom.xml 开头是下面这样的谁能解释下,这东西有社么用,不写这东西又会怎么样的,官方拷贝来的说明文档就算了,我想要简明扼要的说明.不胜感激---------<projec ...

  8. 一个.net mvc的例子

    控制器 ( Controller) Product 下面功能主要根据多条件搜索产品的列表的功能 public ActionResult ProductList(string cityID, strin ...

  9. hdu5878 I Count Two Three(二分+ 打表)

    题目链接:hdu5878 I Count Two Three 题意:给出一个整数n, 找出一个大于等于n的最小整数m, 使得m可以表示为2^a * 3^b * 5^c * 7^d​​. 题解:打表预处 ...

  10. oracle第一章

    1.oracle对比sqlserver oracle sqlserver 数据文件.dbf 数据文件.mdf 控制文件.ctl   日志文件.log 日志文件.log     2.内置用户 1.sys ...