JS常见的string方法
一.监测字符串长度
var a='213412'console.log(a.length) //6
二:JavaScript中常用的string对象的方法
属性 | 说明 |
---|---|
charAt() | 返回指定位置的字符串,空格占位 |
replace() | 用字符替代另一些字符 |
split() | 把一个字符串分割成字符串数组 |
indexOf() | 返回某个指定的字符串 在字符串中首次出现的位置,没有出现返回-1 |
lastIndexOf() | 返回某个指定字符串最后出现的位置 |
match() | 匹配,在字符串中检索指定的值 ,没有为null |
toLowerCase() | 转换为小写 |
toUpperCase() | 转化为大写 |
slice() | 数组和字符串都能用,提取某个字符串的某一部分,并以新的字符串返回并提取的部分 |
substr() | 从索引号提取指定数目的字符(开始位置,提取数量) |
substring() | 同slice() |
trim() | 删除前置和后置的空格 |
toFixed() | 保留小数 |
search() | 检索字符串中,指定的字符串,或者是说,检索与正则表达式匹配的字符串 |
方法如下:
charAt()
var txt='hello world'; console.log(txt.charAt()); //hconsole.log(txt.charAt(4)); //o
replace()
var txt='hello world';txt=txt.replace('hello','happy');console.log(txt) //happy world
split()
var txt='hello world';console.log(txt.split('')); //按照所写内容分割//输出结果:['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
indexOf()
var txt='hello world';console.log(txt.indexOf('hello')); //0console.log(txt.indexOf('world')); //6console.log(txt.indexOf('1')); //-1
lastIndexOf()
var txt='hello world hello world ';console.log(txt.lastIndexOf('hello')); //12console.log(txt.lastIndexOf('world')); //18console.log(txt.lastIndexOf('Hello')); //没有找到为-1
match()
var txt='hello world hello world ';console.log(txt.match('hello'));console.log(txt.match('Hello'));
toLowerCase()
var txt='hello WORLD HELLO world ';console.log(txt.toLocaleLowerCase())
toUpperCase()
var txt='hello WORLD HELLO world ';console.log(txt.toUpperCase())
slice()
var txt='hello WORLD HELLO world ';console.log(txt.slice(3,7)) //lo W 返回第3-7位之间的字符
substr()
var txt='hello WORLD HELLO world ';console.log(txt.substr(3,7)) //从第三位开始往下提取个
substring()
var txt='hello WORLD HELLO world ';console.log(txt.substring(3,7)) //lo W 返回第3-7位之间的字符
trim()
var txt=' hello WORLD HELLO world ';var aa=txt.trim();console.log(txt) //之间输出console.log(aa) //删除前后置空格输出
toFixed()
var aa=1234.72422;console.log(aa.toFixed(2)) //1234.72 保留两位小数
search()
var txt='hello WORLD HELLO world';document.write(txt.search(/h/))document.write(txt.search(/world/i)) //大小写不敏感的匹配
JS常见的string方法相关推荐
- JS中常见的String方法
对于JS中的字符串(String)我们经常使用,今天总结了一下常见的String方法. 1. length 检测字符串的长度 let str = 'abcdef';console.log(str.le ...
- JS常见正则方法整理
JS常见正则方法整理 目录 文章目录 前言 1. 使用测试方法 - `test` 2. 同时用多种模式匹配文字字符串 - `x|y` 3. 匹配时忽略大小写 - `/x/i` 4. 提取匹配项 - ` ...
- JavaScript -- 时光流逝(三):js中的 String 对象的方法
JavaScript -- 知识点回顾篇(三):js中的 String 对象的方法 (1) anchor(): 创建 HTML 锚. <script type="text/javasc ...
- 常见操作String的方法(截取子串,分割字符串,字符串的替换)
常见操作String的方法(截取子串,分割字符串,字符串的替换) 截取子字符串substring() substring()方法有两种重载形式,一种是从指定位置截取到字符串结尾,另一种是截取指定范围的 ...
- 常见操作String的方法(字符查找,索引查找)
常见操作String的方法(字符查找,索引查找) 在给定的字符串中查找字符或字符串是比较常见的操作.字符串查找分为两种形式:一种是在字符串中获取匹配字符(串)的索引值,另一种是在字符串中获取指定索引位 ...
- js常见的几种页面刷新方法
js常见的几种页面刷新方法如下: 1 history.go(0); 2 location.reload(); 3 location=location; 4 location.assign(locati ...
- js中text方法是啥意识_一盏茶的时间,快速捕获JS中常用的方法(细心整理,持续更新ing)...
不知不觉上班一周辽-趁着大好周末,小编挣扎着从床上爬起来,决定对前端日常编程中常用到的一些方法做一个系统的整合. 有些人或许会觉得忘了百度就完事儿,no no no!这事儿小编真的亲践过好多次,百度一 ...
- JS内存泄漏排查方法(Chrome Profiles)
一.概述 Google Chrome浏览器提供了非常强大的JS调试工具,Heap Profiling便是其中一个.Heap Profiling可以记录当前的堆内存(heap)快照,并生成对象的描述文 ...
- JS内存泄漏排查方法——Chrome Profiles
本文转自JS内存泄漏排查方法--Chrome Profiles 概述 Google Chrome浏览器提供了非常强大的JS调试工具,Heap Profiling便是其中一个.Heap Profilin ...
最新文章
- CGpoint,CGSize,CGRect,NSRange
- linux 的ss看进程号,linux ss命令详解
- java curator_[java,zk]在 linux 上快速搭建 zookeeper curator 开发环境
- springboot使用定时器及发送邮件
- Python零碎知识(2):强大的zip
- linux下mysql安装
- html普通幻灯片效果,超酷的CSS3幻灯片效果-前端开发博客_html/css_WEB-ITnose
- eps如何建立立体白模_服装立体裁剪教程 结构都是“立裁”出来的 才智服装
- 洛谷 U3357 C2-走楼梯
- java实现创建窗口
- How to Easily Read a Linux Man Page
- 新书预告<******笔记>快赏
- 音乐类APP竞品分析报告 酷狗音乐 QQ音乐酷我音乐网易云音乐
- linux安装java运行环境_如何安装java运行环境
- Android webview拦截请求
- 1507. 旅行计划
- HTML开心人人新浪微薄等
- 【有限元仿真】平行板电容器仿真
- PS制作黑暗墙面上的漂亮霓虹文字
- matlab如何弄上标,[转载]在Matlab中怎样输入特殊符号或者上标、下标