这个StringReplace代码有什么问题?
我有这个字符串这个StringReplace代码有什么问题?,replace,autohotkey,Replace,Autohotkey,我有这个字符串XXX:ABC。我想删除XXX:,使字符串变成ABC 变量Symbol包含字符串XXX:ABC 守则如下: MsgBox, Symbol %Symbol% SearchText := "XXX:" ReplaceText := "" StringReplace, newSymbol, Symbol, SearchText, ReplaceText, ALL MsgBox, newSymbol %newSymbol% 从消息框输出中,newSymbol内容与Symbol相同。有人
XXX:ABC
。我想删除XXX:
,使字符串变成ABC
变量Symbol
包含字符串XXX:ABC
守则如下:
MsgBox, Symbol %Symbol%
SearchText := "XXX:"
ReplaceText := ""
StringReplace, newSymbol, Symbol, SearchText, ReplaceText, ALL
MsgBox, newSymbol %newSymbol%
从消息框输出中,newSymbol
内容与Symbol
相同。有人能告诉我我的代码有什么问题吗
我正在使用自动热键v1.1.14.03 对于命令参数,必须区分变量参数和值参数。 例如,
StringReplace
具有以下参数列表:
StringReplace,OutputVar,InputVar,SearchText[,ReplaceText,
全部替换?]
文件还说:
OutputVar:用于存储结果的变量的名称
更换过程的详细信息
InputVar:将读取其内容的变量的名称
从
SearchText:要搜索的字符串
如您所见,一些参数应为变量名,而其他参数应为值,如字符串或数字。您可以将变量内容作为值参数使用,方法是将它们用百分号括起来,或在表达式中使用它们:
StringReplace, newSymbol, Symbol, %SearchText%, %ReplaceText%, ALL
; or as an expression
StringReplace, newSymbol, Symbol, % SearchText, % ReplaceText, ALL
对于命令参数,必须区分变量参数和值参数。 例如,
StringReplace
具有以下参数列表:
StringReplace,OutputVar,InputVar,SearchText[,ReplaceText,
全部替换?]
文件还说:
OutputVar:用于存储结果的变量的名称
更换过程的详细信息
InputVar:将读取其内容的变量的名称
从
SearchText:要搜索的字符串
如您所见,一些参数应为变量名,而其他参数应为值,如字符串或数字。您可以将变量内容作为值参数使用,方法是将它们用百分号括起来,或在表达式中使用它们:
StringReplace, newSymbol, Symbol, %SearchText%, %ReplaceText%, ALL
; or as an expression
StringReplace, newSymbol, Symbol, % SearchText, % ReplaceText, ALL
对于命令参数,必须区分变量参数和值参数。 例如,
StringReplace
具有以下参数列表:
StringReplace,OutputVar,InputVar,SearchText[,ReplaceText,
全部替换?]
文件还说:
OutputVar:用于存储结果的变量的名称
更换过程的详细信息
InputVar:将读取其内容的变量的名称
从
SearchText:要搜索的字符串
如您所见,一些参数应为变量名,而其他参数应为值,如字符串或数字。您可以将变量内容作为值参数使用,方法是将它们用百分号括起来,或在表达式中使用它们:
StringReplace, newSymbol, Symbol, %SearchText%, %ReplaceText%, ALL
; or as an expression
StringReplace, newSymbol, Symbol, % SearchText, % ReplaceText, ALL
对于命令参数,必须区分变量参数和值参数。 例如,
StringReplace
具有以下参数列表:
StringReplace,OutputVar,InputVar,SearchText[,ReplaceText,
全部替换?]
文件还说:
OutputVar:用于存储结果的变量的名称
更换过程的详细信息
InputVar:将读取其内容的变量的名称
从
SearchText:要搜索的字符串
如您所见,一些参数应为变量名,而其他参数应为值,如字符串或数字。您可以将变量内容作为值参数使用,方法是将它们用百分号括起来,或在表达式中使用它们:
StringReplace, newSymbol, Symbol, %SearchText%, %ReplaceText%, ALL
; or as an expression
StringReplace, newSymbol, Symbol, % SearchText, % ReplaceText, ALL
对于较新的StrReplace()函数,我注意到无论出于何种原因,我都无法将其用于变量。以及此处的文档:
他缺乏榜样。经过很多测试,我都搞不懂。
因此,我为StrReplace编写了一个“polyfill”,并完成了测试代码
; Author: John Mark Isaac Madison
; EMAIL : J4M4I5M7@hotmail.com
; I_SRC : input source text
; I_OLD : old token to find
; I_NEW : new token to replace old with
FN_POLYFILL_STR_REPLACE(I_SRC, I_OLD, I_NEW)
{
;Check length of input parameters:
;--------------------------------------------;
L1 := StrLen(I_SRC)
L2 := StrLen(I_OLD)
L3 := StrLen(I_NEW)
if( !(L1 > 0) )
{
msgbox BAD_PARAM_#1:STR_REP
}
if( !(L2 > 0) )
{
msgbox BAD_PARAM_#2:STR_REP
}
if( !(L3 > 0) )
{
msgbox BAD_PARAM_#3:STR_REP
}
;--------------------------------------------;
OP := "" ;output string
f_ptr := 0 ;fill pointer
max_i := StrLen(I_SRC)
dx := 0 ;<--Loop counter / index
LOOP ;;[LOOP_START];;
{
dx++
if(dx > max_i)
{
break ;[BAIL_OUT_OF_LOOP]
}
h := FN_IS_TOKEN_HERE(I_SRC, I_OLD, dx)
;if(8==dx)
;{
; msgbox, HACK_8 dx[%dx%] h[%h%] I_SRC[%I_SRC%] I_OLD[%I_OLD%]
; src_len := StrLen( I_SRC )
; old_len := StrLen( I_OLD )
; msgbox src_len [%src_len%] old_len[%old_len%] I_OLD[%I_OLD%]
;}
if( h > 0)
{
;token found, replace it by concating
;the I_NEW onto output string:
OP := OP . I_NEW
;OP := OP . "[X]"
;msgbox : I_SRC[%I_SRC%] h[%h%] dx[%dx%]
;jump pointer to last character of
;the found token to skip over
;now irrelevant characters:
dx := h
;msgbox, DX: %dx%
}
else
if( 0 == h)
{
msgbox, "H_SHOULD_NOT_BE_ZERO"
}
else
if( h < 0 )
{
;concat character to output:
c := SubStr(I_SRC,dx,1)
OP := OP . c
}
} ;;[LOOP_END];;
msgbox OP : %OP%
;msgbox I_SRC[ %I_SRC%] I_OLD[ %I_OLD%] I_NEW[ %I_NEW%]
return OP ;;<--return output string
}
;Author: John Mark Isaac Madison
;EMAIL : J4M4I5M7@hotmail.com
;unit-test that will run when script boots up:
FN_POLYFILL_STR_REPLACE_TEST()
{
T1 := FN_POLYFILL_STR_REPLACE("WHAT_IS_UP","UP","DOWN")
;;msgbox, T1 : %T1%
i_src := "123_TOKEN_123"
i_old := "TOKEN"
i_new := "NEEEW"
T2 := FN_POLYFILL_STR_REPLACE(i_src,i_old,i_new)
;;msgbox, T2 : %T2%
;;msgbox, "POLYFILL_TEST_RAN"
i_src := "const IS_VARNAME"
i_old := "VARNAME"
i_new := "BASH"
T3 := FN_POLYFILL_STR_REPLACE(i_src,i_old,i_new)
;msgbox, T3 : %T3%
i_src := "123456VARNAME"
i_old := "VARNAME"
i_new := "AB"
T4 := FN_POLYFILL_STR_REPLACE(i_src,i_old,i_new)
if(T1 != "WHAT_IS_DOWN")
{
msgbox [PSR_TEST_FAIL#1]
}
if(T2 != "123_NEEEW_123")
{
msgbox [PSR_TEST_FAIL#2]
}
if(T3 != "const IS_BASH")
{
msgbox [PSR_TEST_FAIL#3]
}
if(T4 != "123456AB")
{
msgbox [PSR_TEST_FAIL#4]
}
return ;rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr;
}
FN_POLYFILL_STR_REPLACE_TEST()
;作者:约翰·马克·艾萨克·麦迪逊
; 电邮:J4M4I5M7@hotmail.com
; I_SRC:输入源文本
; I_OLD:要找的旧令牌
; I_NEW:用新令牌替换旧令牌
FN_POLYFILL_STR_替换(I_SRC、I_OLD、I_NEW)
{
;检查输入参数的长度:
;--------------------------------------------;
L1:=StrLen(I_SRC)
L2:=StrLen(I_OLD)
L3:=斯特伦(I_新)
如果(!(L1>0))
{
msgbox坏参数1:STR#u REP
}
如果(!(L2>0))
{
msgbox坏参数2:STR#u REP
}
如果(!(L3>0))
{
msgbox坏参数3:STR#u REP
}
;--------------------------------------------;
OP:=“”;输出字符串
f_ptr:=0;填充指针
max_i:=StrLen(i_SRC)
dx:=0;最大值(i)
{
中断;[跳出循环]
}
h:=FN\u是这里的令牌(I\u SRC,I\u OLD,dx)
;如果(8==dx)
;{
;msgbox,HACK_8 dx[%dx%]h[%h%]I_SRC[%I_SRC%]I_OLD[%I_OLD%]
;src_len:=StrLen(I_src)
;old_len:=StrLen(I_old)
;msgbox src_len[%src_len%]old_len[%old_len%]I_old[%I_old%]
;}
如果(h>0)
{
;找到令牌,请将其替换为concating
;输入新的输出字符串:
OP:=OP.I_新
;OP:=OP.“[X]”
;msgbox:I_SRC[%I_SRC%]h[%h%]dx[%dx%]
;将指针跳转到的最后一个字符
;找到要跳过的令牌
;现在是无关字符:
dx:=h
;msgbox,DX:%DX%