【安卓按鍵精靈】幾種字符串提取的方法(源碼)

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

TracePrint GetStrAB("如果想要寫成一行代碼,那么就可以用冒號連接","想要","代碼")

Function GetStrAB(str,StrA,StrB)

? ? If UTF8.InStr(1, str, StrA)>0 and 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) > 0 and 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 if

End Function

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

Dim arr=GetStrArr("如果(想要)寫成一行(代碼),那么就(可以)用冒號連接","(",")")

For Each k In arr

? ? TracePrint k

Next

Function GetStrArr(str, StrA, StrB)

? ? If UTF8.InStr(1, str, StrA) > 0 and UTF8.InStr(1, str, StrB) > 0 Then

? ? ? ? Dim str_arr=array()

? ? ? ? Dim n=0

? ? ? ? Dim 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+1

? ? ? ? ? ? End If

? ? ? ? Next

? ? ? ? GetStrArr=str_arr

? ? end if

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))) < 91 or 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,"")

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

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。