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的方法进行转换
随机推荐
- Linux下监控磁盘使用量并在超过阀值后自动发送报警邮件
最近Linux服务器磁盘使用量经常到100%,直到影响到正常服务出现故障才会去注意,做不到防患于未然,今天在网上搜集了资料,加上自己修改,写了一个shell脚本用于实时监控磁盘使用量并在超过阀值后自动 ...
- Nginx 上的 php-fpm 资源侵占问题
测试人员向我们反映:在Facebook平台的游戏比其它平台的游戏明显更慢.我询问,是不是因为FQ网络原因.他们说:不是,其它游戏也比较流畅.使用httpwatch查看了http请求,发现api.php ...
- 【转】简单的虚拟摇杆控制移动(NGUI)
http://www.cnblogs.com/zhangbaochong/p/4928688.html 一.用NGUI创建虚拟摇杆贴图 先创建一个sprite作为背景叫做JoyStick 并添加一个B ...
- ubuntu如何开启root,如何启用Ubuntu中root帐号
jingyan.baidu.com/article/495ba84116104238b20ede62.html ubuntu如何开启root,如何启用Ubuntu中root帐号 | 浏览:8344 | ...
- Python排列组合实验
import itertools 排列: 4个数内选2个 >>> print list(itertools.permutations([1,2,3,4],2)) [(1, 2), ( ...
- Mongoose中关联查询populate的使用
MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...
- C# 时间与时间戳互转 13位
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="t ...
- ios9 之后,Xcode7不推荐使用UIAlertView,改用UIAlertController+UIAlertAction(按钮)
/** * ios9 之后,Xcode7不推荐使用UIAlertView,改用UIAlertController+UIAlertAction(按钮) */ UIAlertController *al ...
- cname和CDN
http://blog.csdn.net/crazw/article/details/8986504 先说一下DNS的几个基本概念: 一. 根域 就是所谓的“.”,其实我们的网址www.baidu.c ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...