安卓按鍵精靈 幾種字串提取的方法(原始碼)

2021-10-09 02:31:35 字數 3947 閱讀 9543

擷取法提取兩個字串之間的內容

traceprint getstrab

("如果想要寫成一行**,那麼就可以用冒號連線"

,"想要"

,"**"

)function getstrab

(str,stra,strb)

if utf8.

instr(1

, str, stra)

>

0and utf8.

instr(1

,str,strb)

>

0 then

dim m=utf8.

instr(1

,str,stra)

dim n=utf8.

instr

(m,str,strb)

getstrab=utf8.

mid(str,m+utf8.

len(stra)

,n-m-utf8.

len(stra)

)end if

end function

分割法提取字串

traceprint splitstrab

("如果想要寫成一行**,那麼就可以用冒號連線"

,"想要"

,"**"

)function splitstrab

(str, stra, strb)

if utf8.

instr(1

, str, stra)

>

0and utf8.

instr(1

, str, strb)

>

0 then

dim arr_a=

split

(str,stra)

dim arr_b=

split

(arr_a(1

),strb)

splitstrab=

arr_b(0

)end

ifend function

取多組兩個字串之間的內容

dim arr=

getstrarr(,

"(",

")")

for each k in arr

traceprint k

next

function getstrarr

(str, stra, strb)

if utf8.

instr(1

, str, stra)

>

0and utf8.

instr(1

, str, strb)

>

0 then

dim str_arr=

array()

dim n=

0dim arr_a=

split

(str,stra)

dim arr_b

for i =

1 to ubound

(arr_a)

if instr(1

,arr_a

(i),strb)

>

0 then

arr_b =

split

(arr_a

(i), strb)

str_arr

(n)=

arr_b(0

)n=n+

1end if

next

getstrarr=str_arr

endif

end function

提取數字

traceprint getnum

("如果7991312_ba@326d1b都是a2a693880a25f%1330b955526連線"

)function getnum

(str)

dim num

for i =

1 to utf8.

len(str)

if isnumeric

(utf8.

strgetat

(str,i)

) then

num=num&utf8.

strgetat

(str,i)

end if

next

getnum=num

end function

提取字母

traceprint getzm

("如果7991312_ba@326d1b都是a2a693880a25f%1330b955526連線"

)function getzm

(str)

dim zm

for i =

1 to utf8.

len(str)

if 64

<

cint

(asc

(utf8.

strgetat

(str, i)))

<

91or

96<

cint

(asc

(utf8.

strgetat

(str, i)))

<

123 then

zm=zm&utf8.

strgetat

(str,i)

end if

next

getzm=zm

end function

提取漢字

traceprint getcn

("如果7991312_ba@326d1b都是a2a693880a25f%1330b955526連線"

)function getcn

(str)

dim cn

for i =

1 to utf8.

len(str)

if len

(utf8.

strgetat

(str, i))=

3 then

cn=cn&utf8.

strgetat

(str, i)

end if

next

getcn=cn

end function

正則提取數字

import"shanhai.lua"

dim str=

"如果7991312_ba@326d1b都是a2a693880a25f%1330b955526連線"

dim arr= shanhai.

regexfind

(str,

"%d+"

)traceprint join

(arr,

"")

正則提取字母

import"shanhai.lua"

dim str=

"如果7991312_ba@326d1d都是a2a693880a25f%1330b955526連線"

dim arr= shanhai.

regexfind

(str,

"%a+"

)traceprint join

(arr,

"")

正則提取漢字

import"shanhai.lua"

dim str=

"如果7991312_ba@326d1d都是a2a693880a25f%1330b955526連線"

dim arr= shanhai.

regexfind

(str,

"[\128-\254]+"

)traceprint join

(arr,

"")

本期文章是原始碼分享的形式,感興趣的朋友可以複製原始碼在按鍵中執行一下,自己照著去寫寫就可以學會。

按鍵精靈手機助手之字串處理

獲取ansi碼 獲取字串 查詢字串 合成字串 轉小寫轉大寫 獲取字串字數 取指定字串 替換字串 比較字串 命令名稱 asc 獲得ansi碼 命令功能 返回與字串的第乙個字母對應的 ansi 字元 語法格式 asc string 引數說明 string 是任意有效的字串表示式 返回值 整數型 與字串的...

linux shell中字串相等的幾種方法

3 上面簡單的三個小例項是shell條件測試中的字元測試 eq 測試兩個整數是否相等 比如 a eq b ne 測試兩個整數是否不等 不等,為真 相等,為假 gt 測試乙個數是否大於另乙個數 大於,為真 否則,為假 lt 測試乙個數是否小於另乙個數 小於,為真 否則,為假 ge 大於或等於 le 小...

詳解Python3中字串中的數字提取方法

逛到乙個有意思的部落格在裡面看到一篇關於valueerror invalid literal for int with base 10錯誤的解析,針對這個錯誤,博主已經給出解決辦法,使用的是re.sub 方法 totalcount 100abc totalcount re.sub d totalco...