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 实现(去除字符串首尾指定字符)的更多相关文章

  1. JavaScript trim 实现去除字符串首尾指定字符的简单方法

    String.prototype.trim = function (char, type) { if (char) { if (type == 'left') { return this.replac ...

  2. 用Oracle的TRIM函数去除字符串首尾指定字符

    去掉首尾空格 SELECT TRIM(' abc '), ltrim(' abc '), rtrim(' abc ') FROM dual; 去掉首尾的其他字符 SELECT /*TRIM(';a;b ...

  3. trim()方法去除字符串首尾空格

    trim()方法去除字符串首尾空格 1.原生js                 Function trimStr(str){                         Return str.r ...

  4. 【iOS】去除字符串首尾空格或某字符

    在iOS的实际开发中,常会出现需要去除空格的情况,总结有三种情况: 去除字符串首尾连续字符(如空格): 去除字符串首部连续字符(如空格): 去除字符串尾部连续字符(如空格): 去除字符串首尾连续字符( ...

  5. 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法

    1.利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 首先判断字符串的长度是否为0,如果是,直接返回字符串 第二,循环判断字符串的首部是否有空格,如 ...

  6. python练习题:利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法

    方法一: # -*- coding: utf-8 -*- # 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: def trim(s): whil ...

  7. 【Python实践-4】切片操作去除字符串首尾的空格

    #利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 def trim(s): while s[0:1]==' ': s=s[1:] while s[ ...

  8. C#去掉字符串头尾指定字符

    private void button2_Click(object sender, EventArgs e)        {//去掉字符串头尾指定字符            string MyInf ...

  9. [py]str list切片-去除字符串首尾空格-递归思想

    取出arr的前几项 #方法1 print([arr[0], arr[1]]) #方法2 arr2 = [] for i in range(2): arr2.append(arr[i]) print(a ...

随机推荐

  1. How to center a div in bootstrap3

    There are two approaches to centering a column <div> in Bootstrap 3: Approach 1 (offsets): The ...

  2. AppScan在项目中的使用流程

    AppScan在项目中的使用流程 http://www.docin.com/p-829022229.html

  3. 用js对象创建链表

    //以下是一个链表类 function LinkedList(){ //Node表示要加入列表的项 var Node=function(element){ this.element=element; ...

  4. dedecms获取字段

    在详情页中调用字段使用{dede:field name='title’/} 在列表页调用字段使用: {dede:list} 我是标题:[field:title/],我的的url:[field:youk ...

  5. JAXB - XML Schema Types, Defining Types for XML Elements Without Content

    Types for XML elements are constructed using xsd:complexType, even if they do not have content. The ...

  6. sql标准化的后缀

    今天在SQL编码风格中看到的sql编码标准

  7. OC 将NSString写入本地文件

    最近在公司偶尔遇到一些不经常复现的bug,为了调试,只好把关键值记录到本地文件中,在遇到问题时,调出本地文件查看一下就可以很方便的知道是不是代码逻辑的错误或者问题考虑不够周全了. 废话不多说,流程在代 ...

  8. Java多线程间通信-解决安全问题、等待唤醒机制

    /*1.增加一个知识点一个类怎么在所有的类中,让其它类来共同修改它的数据呢?可以用单例设计模式可以用静态可以在其它类中做一个构造函数,接受同一个对象,这样就可以实现对象 2.状态选择可以用数字0 1 ...

  9. 程序员面试题精选100题(16)-O(logn)求Fibonacci数列[算法]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:定义Fibonacci数列如下: /  0                      n=0 f(n)=      ...

  10. OpenJudge/Poj 1159 Palindrome

    1.链接地址: http://bailian.openjudge.cn/practice/1159/ http://poj.org/problem?id=1159 2.题目: Palindrome T ...