博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String类型方法
阅读量:4685 次
发布时间:2019-06-09

本文共 2378 字,大约阅读时间需要 7 分钟。

String类型

//1、返回长度 lengthvar a="lynn_hello";console.log(a.length);     //10
//2、相加  concat()  返回一个新的字符串var a="123",b="456";console.log(a.concat(b));     //123456
//3、返回字符串所在位置  indexOf()  如果没有匹配项,返回 -1 var a="123456";console.log(a.indexOf("3"));     //2console.log(a.indexOf('3',4)) //-1,从第4个位置搜索
//4、返回指定位置的字符  charAt()  返回字符串所在位置,如果没有匹配项,返回 -1 var a="lynn_hello";console.log(a.charAt("5"));     //h
//5、返回字符串所在位置,从后往前  lastIndexOf()  返回字符串所在位置,如果没有匹配项,返回 -1 var a="lynn_hello";console.log(a.lastIndexOf("o"));     //9
//6、返回字符串的一个子串  substring()  传入参数是起始位置和结束位置var a="lynn_hello";console.log(a.substring(2));     //nn_hello  从第二个往后所有console.log(a.substring(2,4));   //nn   从第二个到第四个,不包括最后一个console.log(a.substring(-5));    //负数返回全部字符串
//7、返回字符串的一个子串  substr()  传入参数是起始位置和长度var a="lynn_hello";console.log(a.substr(2));     //nn_hello  从第二个往后所有console.log(a.substr(2,4));   //nn_h   从第二个开始,往后四个console.log(a.substr(-2));    //lo     a.length+(-2)=5,从5位开始
//8、替换  replace()  var a="lynn_hello";console.log(a.replace("o","#"));     //lynn_hell#
//9、查找  search()  //类似于indexOf()var a="lynn_hello";console.log(a.search("n"));     //2console.log(a.search("x"));     //-1  没找到,返回-1
//10、提取  slice()  提取字符串的一部分,并返回一个新字符串(与 substring 相同)var a="lynn_hello";console.log(a.slice(2));     //nn_hello  从第二个往后所有console.log(a.slice(2,4));   //nn_h   从第二个开始,往后四个console.log(a.slice(-2));    //lo     a.length+(-2)=5,从5位开始
//11、划分  split()  通过将字符串划分成子串,将一个字符串做成一个字符串数组。var a="lynn_hello";console.log(a.split("n"));     //["ly", "", "_hello"]   变成了数组
//12、大小写  toLowerCase()、toUpperCase()   转大小写var a="lynn_hello";console.log(a.toLowerCase());     //转小写console.log(a.toUpperCase());     //转大写

一些扩展

//去除左边的空格var a="      lynn_hello";String.prototype.LTrim = function(){    return this.replace(/(^\s*)/g, "");}console.log(a.LTrim());//去除右边的空格var a="lynn_hello   ";String.prototype.Rtrim = function(){    return this.replace(/(\s*$)/g, "");}console.log(a.Rtrim());//去除前后空格var a="  lynn_hello   ";String.prototype.Trim = function(){    return this.replace(/(^\s*)|(\s*$)/g, "");}console.log(a.Trim());//是否有效连接String.prototype.isUrl = function(){    return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);}var s="http://www.www.com";console.log(s.isUrl())      //true

在指定位置插入字符串

var s="12345678910";var sp=s.split("");for(var i=1;i

转载于:https://www.cnblogs.com/change-oneself/p/4911663.html

你可能感兴趣的文章
ASPCMS 标签
查看>>
《C++ Primer 4th》读书笔记 第12章-类
查看>>
Mac下搭建Apache+PHP+MySql运行环境
查看>>
WCF消息传递
查看>>
测试准入准出标准
查看>>
区块链学习笔记01(基本介绍)
查看>>
[树形dp] 洛谷 P2634 聪聪可可
查看>>
The version of SQL Server in use does not support datatype 'datetime2' 解决办法
查看>>
JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)...
查看>>
DSAPI多功能组件编程应用-HTTP监听服务端与客户端_指令版
查看>>
Java中的ReentrantLock和synchronized两种锁定机制的对比
查看>>
MySQL锁之二:锁相关的配置参数
查看>>
作品汇总和进度表
查看>>
2018-2019-1 20165301 《信息安全系统设计基础》第五周学习总结
查看>>
EF多个表映射
查看>>
J2EE项目集成SAP的BO报表
查看>>
SpringBoot常用属性配置
查看>>
linux sdcv命令
查看>>
BZOJ4836: [Lydsy1704月赛]二元运算【分治FFT】【卡常(没卡过)】
查看>>
MPU6050开发 -- 数据分析(转)
查看>>