javascript 字符串去空格
1、正则去空格
a.去掉字符串中所有空格
" hello world ".replace(/\s+/g,"");//helloworld
b.去掉字符串左边空格
var str = " hello world ".replace(/^\s*/g,"");//hello world..
c.去掉字符串右边空格
var str = " hello world ".replace(/\s*$/g,"");//...hello world
d.去掉字符串左边和右边空格
var str = " hello world ".replace(/(^\s*)|(\s*$)/g,"");//hello world
可以给String的原型添加方法
String.prototype.Trim = function(){ return this.replace(/(^s*)|(\s*$)/g,''); }
String.prototype.LTrim = function(){ return this.replace(/^s*/g,''); }
String.prototype.RTrim = function(){ return this.replace(/\s*$/g,''); }
然后使用
" hello world".LTrim();
//hello world
"hello world ".RTrim();
//hello world
" hello world ".Trim();
//hello world
javascript 字符串去空格的更多相关文章
- 【SQL】字符串去空格解决方法
一.表中字符串带空格的原因 1,空格就是空格. 2,控制符 显示为 空格. 二.解决方法 第一种情况,去空格的处理的比较简单,Replace(column,' ','') 就可以解决. 第二种情况,解 ...
- Foundation框架的一些实用方法:替换字符串,去空格,反转
//定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组 NSMutableString *str = [NSMutableString stringWithForma ...
- ORACLE对字符串去空格处理(trim)
首先便是这Trim函数.Trim 函数具有删除任意指定字符的功能,而去除字符串首尾空格则是trim函数被使用频率最高的一种.语法Trim ( string ) ,参数string:string类型,指 ...
- JavaScript字符串去除空格
/*字符串去除空格*/ String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, "") ...
- 字符串去空格 java , js和Jquery 方法
1. java方式 String.trim(); 2.js方式 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, "&qu ...
- javascript消除字符串两边空格的两种方式,面向对象和函数式编程。python oop在调用时候的优点
主要是javascript中消除字符串空格,比较两种方式的不同 //面向对象,消除字符串两边空格 String.prototype.trim = function() { return this.re ...
- JavaScript去空格之trim()
<script> var str=" ab cd "; alert("["+str.trim()+"]"); </scri ...
- String字符串操作--切割,截取,替换,查找,比较,去空格.....
字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...
- js使用正则表达式去空格
写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...
随机推荐
- joda time, jackson 与 scala 反射
1. scala 反射,获得所有 field name 可以直接从 case class 获得 field 而不必创建实例 (get fields of a class without an inst ...
- graphicsmagick常用命令
显示图像文件详细信息 gm identify a.jpg 1.更改当前目录下.jpg的尺寸大小,并保存于目录.thumb里面 gm mogrify -output-directory .thumbs ...
- mysql存储引擎ARCHIVE
mysql常用引擎MyISAM和InnoDB,前者插入快 查询快,后者修改快 支持事务,各有优缺点,在网上突然看到一个引擎叫ARCHIVE,还蛮特别的 这个引擎只允许插入和查询,不允许修改和删除.相当 ...
- Bypass ngx_lua_waf SQL注入防御(多姿势)
0x00 前言 ngx_lua_waf是一款基于ngx_lua的web应用防火墙,使用简单,高性能.轻量级.默认防御规则在wafconf目录中,摘录几条核心的SQL注入防御规则: select.+ ...
- PHP代码审计笔记--变量覆盖漏洞
变量覆盖指的是用我们自定义的参数值替换程序原有的变量值,一般变量覆盖漏洞需要结合程序的其它功能来实现完整的攻击. 经常导致变量覆盖漏洞场景有:$$,extract()函数,parse_str()函数, ...
- java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available
好久没有使用MyEclipse10了,今天打开看了以前大学的项目,在Tomcat7中发布启动,我嚓嘞,报错: SEVERE: Exception initializing random number ...
- Python入门 学习笔记
十六进制:0x123 布尔运算:and, or, not 空值:None 注释:# raw字符串不需要转义:r'XXX' 多行字符:'''XXX''' 多行字符+raw字符串:r'''XXX''' U ...
- pgAdmin III 单表数据的导出导入
看了好几种方法也试验了几次都没成功,终于找到一种比较简单的试验成功的方法,记录下来留作备份. 将表testTable_1里的数据导入到表testTable_2里,如图: 两表的结构相同.表testTa ...
- 【Spring Boot&&Spring Cloud系列】使用Intellij构建Spring Boot和Mybatis项目
一.创建项目 1.File->New->Project->spring initializer 2.勾选Web SQL Template Engines 3.项目生成之后,点击add ...
- css笔记 - 张鑫旭css课程笔记之 line-height 篇
一.line-height line-height: 指两行文字基线之间的距离. 行高200px表示两行文字基线之间的距离是200px: 二.基线:baseline 字母x下边缘的位置 基线是任意线定 ...