JavaScript trim 实现(去除字符串首尾指定字符)
String.prototype.trim = function (char, type) {
if (char) {
if (type == 'left') {
return this.replace(new RegExp('^\\'+char+'+', 'g'), '');
} else if (type == 'right') {
return this.replace(new RegExp('\\'+char+'+$', 'g'), '');
}
return this.replace(new RegExp('^\\'+char+'+|\\'+char+'+$', 'g'), '');
}
return this.replace(/^\s+|\s+$/g, '');
};
// 去除字符串首尾的全部空白
var str = ' Ruchee ';
console.log('xxx' + str.trim() + 'xxx'); // xxxRucheexxx
// 去除字符串左侧空白
str = ' Ruchee ';
console.log('xxx' + str.trim(' ', 'left') + 'xxx'); // xxxRuchee xxx
// 去除字符串右侧空白
str = ' Ruchee ';
console.log('xxx' + str.trim(' ', 'right') + 'xxx'); // xxx Rucheexxx
// 去除字符串两侧指定字符
str = '/Ruchee/';
console.log(str.trim('/')); // Ruchee
// 去除字符串左侧指定字符
str = '/Ruchee/';
console.log(str.trim('/', 'left')); // Ruchee/
// 去除字符串右侧指定字符
str = '/Ruchee/';
console.log(str.trim('/', 'right')); // /Ruchee
JavaScript trim 实现(去除字符串首尾指定字符)的更多相关文章
- JavaScript trim 实现去除字符串首尾指定字符的简单方法
String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...
- 用Oracle的TRIM函数去除字符串首尾指定字符
去掉首尾空格 SELECT TRIM(' abc '), ltrim(' abc '), rtrim(' abc ') FROM dual; 去掉首尾的其他字符 SELECT /*TRIM(';a;b ...
- trim()方法去除字符串首尾空格
trim()方法去除字符串首尾空格 1.原生js Function trimStr(str){ Return str.r ...
- 【iOS】去除字符串首尾空格或某字符
在iOS的实际开发中,常会出现需要去除空格的情况,总结有三种情况: 去除字符串首尾连续字符(如空格): 去除字符串首部连续字符(如空格): 去除字符串尾部连续字符(如空格): 去除字符串首尾连续字符( ...
- 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法
1.利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 首先判断字符串的长度是否为0,如果是,直接返回字符串 第二,循环判断字符串的首部是否有空格,如 ...
- python练习题:利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法
方法一: # -*- coding: utf-8 -*- # 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: def trim(s): whil ...
- 【Python实践-4】切片操作去除字符串首尾的空格
#利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 def trim(s): while s[0:1]==' ': s=s[1:] while s[ ...
- C#去掉字符串头尾指定字符
private void button2_Click(object sender, EventArgs e) {//去掉字符串头尾指定字符 string MyInf ...
- [py]str list切片-去除字符串首尾空格-递归思想
取出arr的前几项 #方法1 print([arr[0], arr[1]]) #方法2 arr2 = [] for i in range(2): arr2.append(arr[i]) print(a ...
随机推荐
- How to install VXDIAG Honda, Toyota and JLR SDD software
EOBD2 has newly launched Allscanner VXDIAG multi diagnostic tool with OEM diagnostic software: Honda ...
- SQL Server 内存管理
windows memory: Memory: Cache Bytes 是系统的working set, 也就是系统使用的物理内存数目. 可以观察Windows用了多少物理内存. 1. System ...
- saltstack实战2--远程执行之返回(returner)
saltstack有3大功能:远程执行,配置管理,云管理 其中远程执行又可分解为:目标,模块,返回 这3个部分. 比如下面语句 [root@master ~]# salt '*' test.ping ...
- highcharts 24小时显示数据,显示00:00格式的数据
Showing 24 hours on xAxis WITHOUT a date on 00:00 格式参考PHP手册中的: strftime
- jquery图片滑动联播效果
<head> <script src="../Scripts/jquery-1.10.2.js"></script> <meta char ...
- 【C#4.0图解教程】笔记(第19章~第25章)
第19章 泛型 1.泛型概念 泛型提供了一种更准确地使用有一种以上的类型的代码的方式. 泛型允许我们声明类型参数化的代码,我们可以用不同的类型进行实例化. 泛型不是类型,而是类型的模板. 2.声明 ...
- arguments 函数内部属性
1.arguments 是在function方法里面的,是实参数组,用法是挺多的,下面来记录一下 2.利用arguments实现方法的重载 //01.使用argument模拟方法重载 function ...
- ubuntu中下运行asp.net程序
首先在ubuntu下面是不能直接运行VISUAL STUTIO的,必须借助mono开发工具和xsp4.0.那我们就来看一下在ubuntu的电脑中怎么安装这两个工具. 首先安装mono,打开终端,输入a ...
- ### 学习《C++ Primer》- 6
Part 6: 拷贝控制(第13章) // @author: gr // @date: 2015-01-08 // @email: forgerui@gmail.com 一.拷贝.赋值与销毁 拷贝构造 ...
- 第22条:理解NSCopying协议
如果想自定义类支持拷贝操作,那就要实现NSCopying协议(而不是复写copy方法)或 NSMutableCopying的协议. 不可变版本的拷贝: NSCopying协议,该协议只有一个方法: - ...