[H2] str() 字符串操作常用函数

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效
arcticir
Posts: 543
Joined: 17 Nov 2013, 11:32

[H2] str() 字符串操作常用函数

26 Nov 2016, 23:10

Code: Select all

str_in		根据首尾条件提取字符串
str_replace	根据首尾条件替换字符串
str_list		根据首尾条件提取所有字符串到对象
Str_Fill		填充 如1填充4位获得0001
Str_Insert	分割并插入新字符串,最初用于查看十六进制字符串
str_fold		多阶分割提取到对象,如解析INI数据到对象,解析http Headers到对象
Str_Cutting 	根据中间条件提取首尾到对象
Str_LenSplit	根据长度分割字符串
str_LenExtract	提取指定长度的字符串到对象 str_LenExtract(s,3,4,1,88....)

Code: Select all

str_in(ByRef s,i,n,r:=1){
	Return e:=StrLen(i),(b:=(t:=InStr(s,i,,,r)) and (p:=InStr(s,n,,u:=t+e))?SubStr(s,u,p-u):"")?(t:=InStr(b,i,,-1))?substr(b,t+e):b:""
}

str_replace(ByRef s,i,n,r,p:=1){
	e:=StrLen(i)
	if (x:=InStr(s,i,,,p)) and (y:=InStr(s,n,,u:=x+e))
	{
		if k:=InStr(SubStr(s,u,y-u),i,,-1)
			x:=x+k
		Return SubStr(s,1,x+e-1) r SubStr(s,y)
	}
}

str_list(ByRef h,s,u,ByRef f:=""){
	f?"":f:=[],(b:=StrSplit(h,s)).Delete(1)
	for i,n in b
		(r:=InStr(n,u))?f.Push(SubStr(n,1,r-1)):""
	Return f.MaxIndex()?f:""
}

Str_Fill(s,n:=6){
	Return substr("000000000000" s,-n)
}

Str_Insert(s,n,i:="`n"){
	loop Ceil(strlen(s)/n)
		f.=(substr(s,(A_Index-1)*n+1,n)) i
	Return f
}

str_fold(Str,one,two,Omit:="`r `t",f:="",Interval:=" "){
	f?"":f:=[],len:=strlen(two)
	Loop, parse,% Str,% one, % Omit
		(s:=InStr(r:=A_LoopField,two))
			? f[p:=Trim(SubStr(r, 1,s-1),Omit)]?f[p].=Interval Trim(SubStr(r, s+len),Omit):f[p]:=Trim(SubStr(r, s+len),Omit) 
			: f[r]:=""
	return f
}

Str_Cutting(s,n,m:=1,x:=1){
	l:=strlen(n)
	if i:=instr(s,n,,m,x)
	Return [substr(s,1,i-l),substr(s,i+l)]
}

Str_LenSplit(s,n){
	f:=[]
	loop Ceil(strlen(s)/n)
		f.push(substr(s,(A_Index-1)*n+1,n))
	Return f
}

str_LenExtract(s,p*){
	f:=[],r:=1
	for i,n in isobject(p.1)?p.1:p
		f.push(substr(s,r,n) ),r+=n
	return f
}
zhanglei1371
Posts: 19
Joined: 05 Aug 2014, 02:01

Re: [H2] str() 字符串操作常用函数

22 Oct 2017, 05:13

不错,谢谢分享

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 2 guests