用Lua实现string的trim()方法
function trim1(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
-- from PiL2 20.4 function trim2(s)
return s:match "^%s*(.-)%s*$"
end
-- variant of trim1 (match) function trim3(s)
return s:gsub("^%s+", ""):gsub("%s+$", "")
end
-- two gsub's function trim4(s)
return s:match"^%s*(.*)":match"(.-)%s*$"
end
-- variant of trim3 (match) function trim5(s)
return s:match'^%s*(.*%S)' or ''
end
-- warning: has bad performance when s:match'^%s*$' and #s is large function trim6(s)
return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
end
-- fixes performance problem in trim5.
-- note: the '()' avoids the overhead of default string capture.
-- This overhead is small, ~ % for successful whitespace match call
-- alone, and may not be noticeable in the overall benchmarks here,
-- but there's little harm either. Instead replacing the first `match`
-- with a `find` has a similar effect, but that requires localizing
-- two functions in the trim7 variant below. local match = string.match
function trim7(s)
return match(s,'^()%s*$') and '' or match(s,'^%s*(.*%S)')
end
-- variant of trim6 (localize functions) local find = string.find
local sub = string.sub
function trim8(s)
local i1,i2 = find(s,'^%s*')
if i2 >= i1 then s = sub(s,i2+) end
local i1,i2 = find(s,'%s*$')
if i2 >= i1 then s = sub(s,,i1-) end
return s
end
-- based on penlight 0.7. function trim9(s)
local _,i1 = find(s,'^%s*')
local i2 = find(s,'%s*$')
return sub(s,i1+,i2-)
end
-- simplification of trim8 function trim10(s)
local a = s:match('^%s*()')
local b = s:match('()%s*$', a)
return s:sub(a,b-)
end
-- variant of trim9 (match) function trim11(s)
local n = s:find"%S"
return n and s:match(".*%S", n) or ""
end
-- variant of trim6 (use n position)
-- http://lua-users.org/lists/lua-l/2009-12/msg00904.html function trim12(s)
local from = s:match"^%s*()"
return from > #s and "" or s:match(".*%S", from)
end
-- variant of trim11 (performs better for all
-- whitespace string). See Roberto's comments
-- on ^%s*$" v.s. "%S" performance:
-- http://lua-users.org/lists/lua-l/2009-12/msg00921.html do
require 'lpeg'
local space = lpeg.S' \t\n\v\f\r'
local nospace = - space
local ptrim = space^ * lpeg.C((space^ * nospace^)^)
local match = lpeg.match
function trim13(s)
return match(ptrim, s)
end
end
-- lpeg. based on http://lua-users.org/lists/lua-l/2009-12/msg00921.html do
require 'lpeg'
require 're'
local ptrim = re.compile"%s* {(%s* %S+)*}"
local match = lpeg.match
function trim14(s)
return match(ptrim, s)
end
end
-- variant with re module. require 'trim'
local trim15 = trim
-- C implementation (see separate trim.c file) -- test utilities local function trimtest(trim)
assert(trim'' == '')
assert(trim' ' == '')
assert(trim' ' == '')
assert(trim'a' == 'a')
assert(trim' a' == 'a')
assert(trim'a ' == 'a')
assert(trim' a ' == 'a')
assert(trim' a ' == 'a')
assert(trim' ab cd ' == 'ab cd')
assert(trim' \t\r\n\f\va\000b \r\t\n\f\v' == 'a\000b')
end local function perftest(f, s)
local time = os.clock -- os.time or os.clock
local t1 = time()
for i=, do
f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)
end
local dt = time() - t1
io.stdout:write(string.format("%4.1f",dt) .. ' ')
end local trims = {trim1, trim2, trim3, trim4, trim5, trim6, trim7,
trim8, trim9, trim10, trim11, trim12, trim13, trim14, trim15} -- correctness tests
for _,trim in ipairs(trims) do
trimtest(trim)
end -- performance tests
for j=, do
for i,trim in ipairs(trims) do
io.stdout:write(string.format("%2d",i) .. ": ")
perftest(trim, "")
perftest(trim, "abcdef")
perftest(trim, " abcdef ")
perftest(trim, "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef")
perftest(trim, " a b c d e f g h i j k l m n o p q r s t u v w x y z A B C ")
perftest(trim, " a ")
perftest(trim, " ")
print()
end
end
原文地址:http://lua-users.org/wiki/StringTrim
用Lua实现string的trim()方法的更多相关文章
- IE8下String的Trim()方法失效的解决方法
String的Trim()方法失效,在ie8下是有这样的情况的,解决方法也很简单使用$.trim(str)即可,需要的朋友可以了解下 用jquery的trim()方法,$.trim(str)就可以了.
- JS中String添加trim()方法
这么牛的JS竟然还要自己封装trim方法. 下面利用prototype和正则表达式的添加方式添加trim(): <script language="javascript"&g ...
- 自己实现String.prototype.trim方法
今天呢 知乎看到一道题 说是网易面试题,要求自己写一个trim()方法, 实现 var str = " a sd "; 去掉字符串两端的空格. 直接上码 var str ...
- js在IE8+兼容String没有trim方法,写一个兼容ie8一下的浏览器的trim()方法
方法一: String.prototype.trim = function(){ return Trim(this);}; function LTrim(str) { var i; fo ...
- 字符串String的trim()方法
用来删除字符串两端的空白字符并返回,trim方法并不影响原来的字符串本身,它返回的是一个新的字符串 String a = " Hello World "; String b = ...
- IE8下String的Trim()方法失效的解决方案
简洁方便 用jquery的trim()方法,$.trim(str)就可以了.
- spring框架中一个跟String的trim方法一样的方法
@Test public void testTrimWhitespace() throws Exception { assertEquals(null, StringUtils.trimWhitesp ...
- java中String的.trim()方法
该方法去除两边的空白符 原理: 看看源码实现 public String trim() { int len = value.length; ; char[] val = value; /* avoid ...
- java String类 trim() 方法源码分析
public String trim() { int arg0 = this.value.length; //得到此字符串的长度 int arg1 = 0; //声 ...
随机推荐
- 【Unity】角色受伤后的闪烁(blink/flash)效果
玩家受伤后,一段时间内快速闪烁.这里想要的闪烁效果是玩家快速的显隐切换效果,而不是玩家变白的情况. 快速切换玩家的显隐效果不能用SetActive修改角色物体本身的激活状态,因为玩家角色身上的其他脚本 ...
- Java并发编程:并发容器之CopyOnWriteArrayList<转>
原文链接: http://ifeve.com/java-copy-on-write/ Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容 ...
- FileChannel类的理解和使用
FileChannel类的理解和使用(java.nio.channels.FileChannel) 知识点: 1.FileChannel类及方法理解:2.普通输入输出流复制文件:3.FileChann ...
- ssh登陆并执行命令不退出
如果希望SSH登陆后先执行shell命令,可以这样: ssh user@ip -t "cd /data ; /bin/bash"
- c# 文字首字母
public string GetFirstLetter(string hz) { string ls_second_eng = "CJWGNSPGCGNESYPBTYYZDXYKYGTDJ ...
- jdbc读取百万条数据出现内存溢出的解决办法
本人在做项目实施时,我们使用的是mysql数据库,在不到一个月的时间已经有了2千万条数据,查询的时候非常慢,就写了一个数据迁移的小项目,将这两千万条数据存放到MongoDB中看效率怎么样,再读取数据时 ...
- 微信支付WxpayAPI_php_v3(三)支付成功回调
接收回调通知后的业务处理都在NotifyProcess做,$data包含了微信返回给你的数据. Service: <?php /** * Created by PhpStorm. * User: ...
- Mongo的备份和恢复(mongodump 和mongorestore )
http://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html --备份单个表mongodump -u superuser -p 1 ...
- js学习(一)-动态添加、修改、删除对象的属性和方法
//-----------------------js代码--------------------------- function class1(){ } //-------------------- ...
- 表格细边框的两种CSS实现方法
在网页制作中,细边框这个制作方法是必不可少的.这里介绍2种常见的表格细边框制作方法,均通过XHTML验证. <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...