Skip to content

strings

liyonglion edited this page Dec 13, 2016 · 1 revision

#strings ##Hasprefix ishas = strings.Hasprefix(s, prefix) 判断s是否有前缀字符串prefix。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local ishas = strings.Hasprefix("/tmp/test.txt", "/tmp")
if ishas then
print("has prefix")
else
print("not has prefix")
end
--has prefix

##Hassuffix ishas = strings.Hassuffix(s,suffix) 判断s是否有后缀字符串suffix。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local ishas = strings.Hassuffix("/tmp/test.txt", "test.txt")
if ishas then
print("has suffix")
else
print("not has suffix")
end
--has suffix

##Contains iscontain = strings.Contains(s, substr) 判断字符串s是否包含子串substr。

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local iscontain = strings.Contains("/tmp/test.txt", "est")
if iscontain then
print("has Contains")
else
print("not has Contains")
end
--has Contains

##Count count = strings.Count(s, sep) 返回字符串s中有几个重复的sep子串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test. author is lion."
print(strings.Count(a, "is"))--3
print(strings.Count(a, "."))--2
print(strings.Count(a, "wow"))--0

##Fields fieldtb = strings.Fields(s) 将字符串s根据空格、\f、\n、\n、\t、\v分割成表。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "\t this is a te\nst"

local t = strings.Fields(a)
for _,v in ipairs(t) do
	print(v)
end
--this
--is
--a
--te
--st

##Index index = strings.Index(s,sep) 子串sep在字符串s中第一次出现的位置,不存在则返回-1。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is index of test"
print(strings.Index(a, "of"))--15
print(strings.Index(a, "fo"))-- -1

##Indexany index = strings.Indexany(s, charts) 字符串chars中的任一值在s中第一次出现的位置,如果不存在或者chars为空字符串则返回-1 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Indexany(a, "ts"))--1
print(strings.Indexany(a, "wt"))--1
print(strings.Indexany(a, "vs"))--4
print(strings.Indexany(a, "vw"))-- -1

##Lastindex lastindex = strings.Lastindex(s, sep) 子串sep在字符串s中最后一次出现的位置,不存在则返回-1。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Lastindex(a, "is"))--6
print(strings.Lastindex(a, "a"))--9
print(strings.Lastindex(a, "tmp"))-- -1

##Lastindexany lastindexany = strings.Lastindexany(s, chars) 字符串chars中的任一值在s中最后一次出现的位置,如不存在或者chars为空字符串则返回-1。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Lastindexany(a, "tw"))--15
print(strings.Lastindexany(a, "at"))--15
print(strings.Lastindexany(a, "wv"))-- -1

##Repeat repeat = strings.Repeat(s, count) 返回count个s串联的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "Repeat"
print(strings.Repeat(a, 3))--RepeatRepeatRepeat
print(strings.Repeat(a, 0))--空字符串

##Replace replace = strings.Replace(s, old, new, n) 返回将s中前n个不重叠old子串都替换为new的新字符串,如果n<0会替换所有old子串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Replace(a, "is", "are", 1))--thare is a test
print(strings.Replace(a, "is", "are", 2))--thare are a test
print(strings.Replace(a, "is", "are", 3))--thare are a test
print(strings.Replace(a, "is", "are", -1))--thare are a test
print(strings.Replace(a, "are", "is", -1))--this is a test

##Splitstring ss = strings.Splitstring(s, sep) 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的表(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
local a = "thisisatest"
local tb = strings.Splitstring(a, "is")
for _,v in ipairs(tb) do
	print(v)
end
--th
--
--atest
local tb = strings.Splitstring(a, "table")
for _,v in ipairs(tb) do
	print(v)
end
--thisisatest

##Trim trim = strings.Trim(s, cutset) 返回将s前后端所有cutset包含的值都去掉的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "a \r\t this is a test \n\ra"
local b = "a \r\t\n"
print(strings.Trim(a,b))--this is a test

##Trimleft trimleft = strings.Trimleft(s, cutset) 返回将s前端所有cutset包含的值都去掉的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "a \r\t this is a test \ta"
local b = "\r\t a"
print(strings.Trimleft(a,b))---this is a test     a

##Trimprefix trimprefix = strings.Trimprefix(s, prefix) 返回去除s可能的前缀prefix的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Trimprefix(a, "this"))-- is a test
print(strings.Trimprefix(a, "tts"))--this is a test

##Trimright trimright = strings.Trimright(s, cutset) 返回将s后端所有cutset包含的值都去掉的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "a \r\t this is a test \n\ra"
local b = "a \r\t\n"
print(strings.Trimright(a,b))--a        this is a test

##Trimsuffix trimsuffix = strings.Trimsuffix(s, suffix) 返回去除s可能的后缀suffix的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "this is a test"
print(strings.Trimsuffix(a, "test"))--this is a 
print(strings.Trimsuffix(a, "tts"))--this is a test

##Trimspace trimspace = strings.Trimspace(s) 返回将s前后端都去掉空格的字符串。 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local a = "   this is a test	"
print(strings.Trimspace(a))---this is a test

##md5 md_5 = strings.md5(s) 返回字符串s的md5值,32位 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
print(strings.md5("hello"))--5d41402abc4b2a76b9719d911017c592

##base64 base_64 = strings.base64(s) 返回字符串s的base64值 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local b64 = strings.base64("hello")
print(b64)--aGVsbG8=

##unbase64 unb64 = strings.unbase64(s) 返回s的unbase64值 ###example

package.cpath = package.cpath .. ";/usr/local/luaextend/strings/?.so"
local strings = require("strings")
local hello = "aGVsbG8="
print(strings.unbase64(hello))--hello
Clone this wiki locally