Javascript ----字符串(String)中的方法
涉及字符串时,常用到的几个方法...
----------------------------------------------------------------------------------------------------
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)中的方法的更多相关文章
- JavaScript 字符串(String)对象的方法
anchor() 描述:用于创建 HTML 锚 原型:stringObject.anchor(anchorname) 用法: <script> var txt="Hello wo ...
- JS json对象(Object)和字符串(String)互转方法
[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...
- JavaScript字符串String
JavaScript中String类型用于表示由零个或者多个16位Unicode字符组成的字符序列即字符串:同时字符串可以用单引号或双引号表示. 下面是一些特殊的字面量: 字面量 含义\n 换行\t ...
- JavaScript 字符串(String)对象
String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); String(s); 参数 参数 s 是要存储在 String 对象中或 ...
- JavaScript 字符串(String) 大全
JavaScript字符串存储一系列字符,如“John Doe”.字符串可以是双引号或单引号内的任何文本: <!DOCTYPE html> <html> <meta ch ...
- java String 中 intern方法的概念
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- String中concat方法小记
介绍String中的concat方法使用: 日常开发中,经常对字符串进行处理,之前碰到多个字符串拼接,要么使用stringBuilder,要么使用StringBuffer,再或者是直接多个String ...
- String中intern方法的作用
前言 读完这篇文章你可以了解,String对象在虚拟机内存中的存放,intern的作用,这么多String对象的创建到底有什么区别,String 创建的对象有几个!! 正题 先科普几个知识点1.常量池 ...
- C++中 string 中的方法的使用详解
string 字符串在所有的语言中都非常重要,c++也不例外,接下来我们将介绍string中的常用方法 1. size() 和 length() 函数 : 他们返回字符串的真实长度,且不会因为空格而截 ...
随机推荐
- ASP.NET Core文档中Work with Data章节的翻译目录
作为初学者看了相关的教程,遇到的问题有: 1. 教程不是针对初学者,往往在某一方面教的较深,但并不系统,不适合初学者: 2. 虽然翻译的很顺畅,但是谈了自己较多的开发体会,初学者看着困难,尤其是TOM ...
- 【转】 C++中的new VS C语言中的malloc
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 前几天一个朋友去面试百度空间的一个职位,被问及这个问题,我听后说了几点,不过感觉还是不透彻,所以上网查阅了一些资 ...
- IE6 IE7 IE8(Q) 负边距 (margin) 导致元素溢出 hasLayout 容器时显示异常
标准参考 根据W3C CSS2.1规范第8.3节中的描述,边距属性设置了一个框的边距区的宽度.'margin' 缩写属性设置所有四边的边距,而其它的边距属性( 'margin-top' ,'margi ...
- python3.x随手笔记1
语法分析 Python程序读取的 解析器 . 解析器的输入流 令牌 ,生成的 词法分析程序 . 这一章描述了如何 词法分析程序把一个文件分解成令牌. Python读取程序文本作为Unicode代码点; ...
- Github/Eclipse管理Maven项目
Eclipse和Git插件 (To-do: 直接从workspace导入也可以,弄明白这个repo管理的本质,查看sprigmvc是如何导入的) 最新版本的Eclipse都直接集成了Git插件 Ecl ...
- hdu---(1280)前m大的数(计数排序)
前m大的数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- web页面动态加载UserControl,并调用用户控件中的方法来初始化控件
1,HTML页 头部注册: <%@ Register Src="~/WorkLog/WorkLogNewV1/UserControl/CeShiBu.ascx" TagPre ...
- 转--CSS选择器详解(一)常用选择器
今天复习一下CSS的知识,看了篇文章觉得很好,转来备用. 转自:http://www.cnblogs.com/fattydoit/p/3492028.html 目录 类型选择器 类选择器 ID选择器 ...
- Asp.Net 导出Excel数据文件
表格例子如下: <table id="tableExcel" width="100%" border="1" cellspacing= ...
- BZOJ2490 Zombie’s Treasure Chest
如果n = lcm(s1, s2),那么就可以直接得到maxV = (v / s1 * v1, v / s2 *v2) 然后还剩下一点体积我们暴力枚举用s1的量,让s1为max(s1, s2)可以减少 ...