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的方法进行转换
随机推荐
- Allowed memory size Out of memory ini_set('memory_limit', '-1');
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 51 bytes) ini_set(' ...
- java 使用cookie记录用户上一次访问的时间 记住 用户的 登录名
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.D ...
- c#循环迭代匿名类链表(可迭代的匿名类)
Main(){ //为什么?object是基类啊!! //报错.不能从List<anonymous>换成List<object>. //var q=(List<objec ...
- C#5.0 .net 4.5示例
//filename: MathOperations.cs using System; using System.Collections.Generic; using System.Linq; usi ...
- 【C++】函数指针宏定义
看耗子叔文章学习虚函数表(http://blog.csdn.net/haoel/article/details/1948051)的时候被例子的第一句惊到了 typedef void(*Fun)(voi ...
- magento多语言中文语言包
语言包key:http://connect20.magentocommerce.com/community/Mage_Locale_zh_CN
- Hubilder用git插件安装使用
打开Hbuilder,工具->插件安装(git分布式版本管理插件) 打开https://www.github.com,注册.登录.创建仓库 在Hbuilder中新建项目→然后右键→Team→共享 ...
- 配置maven环境
第一步:安装maven,安装maven最简单,直接将maven的解压文件放入本地某目录下即可,无需手动安装 第二步:eclipse中导入maven项目后,会后错,或maven无法使用,则需要进行mav ...
- LeetCode Shortest Word Distance
原题链接在这里:https://leetcode.com/problems/shortest-word-distance/ 题目: Given a list of words and two word ...
- Android Error:Execution failed for task ':app:mergeDebugResources'. > Crunching Cruncher bg_btn.9.png
使用Android Studio来进行图片背景设置,编译时发生了一个mergeDebugResources异常. 异常原因 这个异常的意思是对资源合并发生错误,那就是我使用的图片资源有问题,我使用的图 ...