String类型方法
String类型
//1、返回长度 length
var a="lynn_hello";
console.log(a.length); //
//2、相加 concat() 返回一个新的字符串
var a="123",b="456";
console.log(a.concat(b)); //
//3、返回字符串所在位置 indexOf() 如果没有匹配项,返回 -1
var a="123456";
console.log(a.indexOf("3")); //
console.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")); //
//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")); //
console.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<sp.length;i+=2){
sp[i]+=","
}
sp.join("")
String类型方法的更多相关文章
- java中Object转换成int或String类型方法
转载: http://www.cnblogs.com/1020182600HENG/p/6137206.html Object obj = getObject(); if(obj instanceof ...
- String类型的学习
一 :关于两个string类型变量是否相等: 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 分析: 首先为s0开辟空间,然后给s1开辟 ...
- String类型的属性和方法
× 目录 [1]属性 [2]对象通用方法 [3]访问字符方法[4]字符串拼接[5]创建子串方法[6]大小写转换[7]查找子串位置[8]正则匹配方法[9]去除首尾空格[10]字符串比较 前面的话 前面已 ...
- 将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件
将String类型的二维数组中的元素用FileOutputStream的write方法生成一个文件import java.io.File;import java.io.FileOutputStre ...
- 判断String类型字符串是否为空的方法
在项目中经常遇到要判断String类型的字段是否为空操作 我们可以用Apache提供的StringUtils这个工具类,不用自己去判断,也不用自己封装判断空的方法 它有两个版本,一个是org.apac ...
- String类型作为方法的形参
代码: public class TestString { String str = new String("good"); char [] ch = {'a','b','c'}; ...
- String 类型equals方法和int == 方法效率比较
最近写了一个递归方法,在进行比较判断的时候,因为都是integer类型,而integer类型在大于127或者小于-128时会在新建一个,这是因为integer类型的拆装箱机制, 之前没有考虑过equa ...
- String类型的方法总结
String :字符串对象的包装类型 var stringObject = new String("wanglehui"); 方法总结: 1.返回该对象表示的基本字符串值(也就是返 ...
- 当要将其他类型转成String类型时候 看String的方法
当要将其他类型转成String类型时候 看String的方法进行转换
随机推荐
- Cluster analysis
https://en.wikipedia.org/wiki/Cluster_analysis Cluster analysis or clustering is the task of groupin ...
- communication between threads 线程间通信 Programming Concurrent Activities 程序设计中的并发活动 Ada task 任务 Java thread 线程
Computer Science An Overview _J. Glenn Brookshear _11th Edition activation 激活 parallel processing 并行 ...
- ubuntu中一些配置文件含义
/var/log/apache2/mod_jk.log apache2 mod_jk错误日志错误 /conf/server.xml ...
- Bluetooth GAP介绍
目录 1 GAP协议栈 2 Profile Role 3 用户接口 4 模式 5 安全 5.1 认证(Authentication) 5.2 安全模式 6 Idle Mode Procedures 7 ...
- Java迭代 : Iterator和Iterable接口
从英文意思去理解 Iterable :故名思议,实现了这个接口的集合对象支持迭代,是可迭代的.able结尾的表示 能...样,可以做.... Iterator: 在英语中or 结尾是都是表示 .. ...
- struts.xml 配置详解
struts.xml是我们在开发中利用率最高的文件,也是Struts2中最重要的配置文件. 一下分别介绍一下几个struts.xml中常用到的标签 1.<include> 利用includ ...
- Windows下使用Git和GitHub.com
1.首先介绍一下什么是Git和GitHub Git是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大 ...
- iOS UIImageView 显示不规则图片只显示图片一部分保证图片不被压缩
//只需如下设置imageView [picImg setContentScaleFactor:[[UIScreenmainScreen] scale]]; picImg.contentMode = ...
- dede完美分页样式
html <div class="dede_pages"> <ul class="pagelist"> ...
- Mysqldump 参数详解(全)
Mysqldump 参数详解(全) http://www.open-open.com/lib/view/open1358172843762.html mysqldump -S /tmp/mysql33 ...