用Lua实现string的trim()方法

  1 function trim1(s)
  2   return (s:gsub("^%s*(.-)%s*$", "%1"))
  3 end
  4 -- from PiL2 20.4
  5
  6 function trim2(s)
  7   return s:match "^%s*(.-)%s*$"
  8 end
  9 -- variant of trim1 (match)
 10
 11 function trim3(s)
 12   return s:gsub("^%s+", ""):gsub("%s+$", "")
 13 end
 14 -- two gsub‘s
 15
 16 function trim4(s)
 17   return s:match"^%s*(.*)":match"(.-)%s*$"
 18 end
 19 -- variant of trim3 (match)
 20
 21 function trim5(s)
 22   return s:match‘^%s*(.*%S)‘ or ‘‘
 23 end
 24 -- warning: has bad performance when s:match‘^%s*$‘ and #s is large
 25
 26 function trim6(s)
 27   return s:match‘^()%s*$‘ and ‘‘ or s:match‘^%s*(.*%S)‘
 28 end
 29 -- fixes performance problem in trim5.
 30 -- note: the ‘()‘ avoids the overhead of default string capture.
 31 -- This overhead is small, ~ 10% for successful whitespace match call
 32 -- alone, and may not be noticeable in the overall benchmarks here,
 33 -- but there‘s little harm either.  Instead replacing the first `match`
 34 -- with a `find` has a similar effect, but that requires localizing
 35 -- two functions in the trim7 variant below.
 36
 37 local match = string.match
 38 function trim7(s)
 39   return match(s,‘^()%s*$‘) and ‘‘ or match(s,‘^%s*(.*%S)‘)
 40 end
 41 -- variant of trim6 (localize functions)
 42
 43 local find = string.find
 44 local sub = string.sub
 45 function trim8(s)
 46   local i1,i2 = find(s,‘^%s*‘)
 47   if i2 >= i1 then s = sub(s,i2+1) end
 48   local i1,i2 = find(s,‘%s*$‘)
 49   if i2 >= i1 then s = sub(s,1,i1-1) end
 50   return s
 51 end
 52 -- based on penlight 0.7.2
 53
 54 function trim9(s)
 55   local _,i1 = find(s,‘^%s*‘)
 56   local i2 = find(s,‘%s*$‘)
 57   return sub(s,i1+1,i2-1)
 58 end
 59 -- simplification of trim8
 60
 61 function trim10(s)
 62   local a = s:match(‘^%s*()‘)
 63   local b = s:match(‘()%s*$‘, a)
 64   return s:sub(a,b-1)
 65 end
 66 -- variant of trim9 (match)
 67
 68 function trim11(s)
 69  local n = s:find"%S"
 70  return n and s:match(".*%S", n) or ""
 71 end
 72 -- variant of trim6 (use n position)
 73 -- http://lua-users.org/lists/lua-l/2009-12/msg00904.html
 74
 75 function trim12(s)
 76  local from = s:match"^%s*()"
 77  return from > #s and "" or s:match(".*%S", from)
 78 end
 79 -- variant of trim11 (performs better for all
 80 -- whitespace string). See Roberto‘s comments
 81 -- on ^%s*$" v.s. "%S" performance:
 82 -- http://lua-users.org/lists/lua-l/2009-12/msg00921.html
 83
 84 do
 85  require ‘lpeg‘
 86  local space = lpeg.S‘ \t\n\v\f\r‘
 87  local nospace = 1 - space
 88  local ptrim = space^0 * lpeg.C((space^0 * nospace^1)^0)
 89  local match = lpeg.match
 90  function trim13(s)
 91    return match(ptrim, s)
 92  end
 93 end
 94 -- lpeg.  based on http://lua-users.org/lists/lua-l/2009-12/msg00921.html
 95
 96 do
 97  require ‘lpeg‘
 98  require ‘re‘
 99  local ptrim = re.compile"%s* {(%s* %S+)*}"
100  local match = lpeg.match
101  function trim14(s)
102    return match(ptrim, s)
103  end
104 end
105 -- variant with re module.
106
107 require ‘trim‘
108 local trim15 = trim
109 -- C implementation (see separate trim.c file)
110
111
112 -- test utilities
113
114 local function trimtest(trim)
115   assert(trim‘‘ == ‘‘)
116   assert(trim‘ ‘ == ‘‘)
117   assert(trim‘  ‘ == ‘‘)
118   assert(trim‘a‘ == ‘a‘)
119   assert(trim‘ a‘ == ‘a‘)
120   assert(trim‘a ‘ == ‘a‘)
121   assert(trim‘ a ‘ == ‘a‘)
122   assert(trim‘  a  ‘ == ‘a‘)
123   assert(trim‘  ab cd  ‘ == ‘ab cd‘)
124   assert(trim‘ \t\r\n\f\va\000b \r\t\n\f\v‘ == ‘a\000b‘)
125 end
126
127 local function perftest(f, s)
128   local time = os.clock  -- os.time or os.clock
129   local t1 = time()
130   for i=1,100000 do
131     f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)f(s)
132   end
133   local dt = time() - t1
134   io.stdout:write(string.format("%4.1f",dt) .. ‘ ‘)
135 end
136
137 local trims = {trim1, trim2, trim3, trim4, trim5, trim6, trim7,
138                trim8, trim9, trim10, trim11, trim12, trim13, trim14, trim15}
139
140 -- correctness tests
141 for _,trim in ipairs(trims) do
142   trimtest(trim)
143 end
144
145 -- performance tests
146 for j=1,3 do
147   for i,trim in ipairs(trims) do
148     io.stdout:write(string.format("%2d",i) .. ": ")
149     perftest(trim,  "")
150     perftest(trim,  "abcdef")
151     perftest(trim,  "   abcdef   ")
152     perftest(trim,  "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef")
153     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 ")
154     perftest(trim,  "                               a                            ")
155     perftest(trim,  "                                                            ")
156     print()
157   end
158 end

原文地址:http://lua-users.org/wiki/StringTrim

时间: 2024-10-09 16:47:33

用Lua实现string的trim()方法的相关文章

IE8下String的Trim()方法失效的解决方法

String的Trim()方法失效,在ie8下是有这样的情况的,解决方法也很简单使用$.trim(str)即可,需要的朋友可以了解下 用jquery的trim()方法,$.trim(str)就可以了.

js在IE8+兼容String没有trim方法,写一个兼容ie8一下的浏览器的trim()方法

String.prototype.trim = function(){ return Trim(this);}; function LTrim(str) { var i;     for(i=0;i<str.length;i++)     {         if(str.charAt(i)!=" "&&str.charAt(i)!=" ")break;     }     str=str.substring(i,str.length);   

spring框架中一个跟String的trim方法一样的方法

@Test public void testTrimWhitespace() throws Exception { assertEquals(null, StringUtils.trimWhitespace(null)); assertEquals("", StringUtils.trimWhitespace("")); assertEquals("", StringUtils.trimWhitespace(" ")); as

java中String的.trim()方法

该方法去除两边的空白符 原理: 看看源码实现 public String trim() { int len = value.length; int st = 0; char[] val = value; /* avoid getfield opcode */ while ((st < len) && (val[st] <= ' ')) { st++; } while ((st < len) && (val[len - 1] <= ' ')) { le

在lua的string库和正则表达式

一.前提要了解一下lua 的string几个方法 1. string库中所有的字符索引从前往后是1,2,...;从后往前是-1,-2,... 2. string库中所有的function都不会直接操作字符串,而是返回一个结果 string.len(s):返回字符串的长度. string.lower(s):变小写. string.upper(s):变大写. string.rep(s,n):将s拷贝n份,并连接起来,返回. string.sub(s,i [,j]):取s中从i开始到j为止的自字符串.

java.lang.String的trim()方法详解

String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗? 一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱. 首先我直接反编译String类,找到Trim()方法: public string Trim() {    return this.TrimHelper(WhitespaceChars, 2); } TrimHelper方法有两个参数,第一个参数名WhitespaceChars,首字母尽然是大写的,肯定有文章,真不出我所料:

java基础知识回顾之---java String final类普通方法的应用之“模拟字符串Trim方法”

/* * 4,模拟一个trim功能一致的方法.去除字符串两端的空白  * 思路: * 1,定义两个变量. * 一个变量作为从头开始判断字符串空格的角标.不断++. * 一个变量作为从尾开始判断字符串空格的角标.不断--. * 2,判断到不是空格为止,取头尾之间的字符串即可. *  *  使用char charAt(int index);方法根据index索引,取出字符串 *  使用String substring(int beginIndex, int endIndex)//包含begin 不包

11.为String类扩展trim方法

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 2 "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"

java.lang.String里面的trim()方法——删除首尾空格

结果如图 package com.softeasy.test1; public class String_trim { public static void main(String[] args) { String str = " slkjfsj sdfjf jsjs "; System.out.println("没有用trim()方法前str为:|" + str + "|"); str = str.trim(); System.out.prin