【问题标题】:What is wrong with this StringReplace code?这个 StringReplace 代码有什么问题?
【发布时间】:2014-06-04 00:08:01
【问题描述】:

我有这个字符串XXX:ABC。我想删除 XXX: 使字符串变为 ABC

变量 Symbol 包含字符串 XXX:ABC

代码如下:

MsgBox, Symbol %Symbol%
SearchText := "XXX:"
ReplaceText := ""
StringReplace, newSymbol, Symbol, SearchText, ReplaceText, ALL 
MsgBox, newSymbol %newSymbol%

从消息框输出来看,newSymbol 的内容与Symbol 相同。谁能告诉我我的代码有什么问题?

我正在使用 Autohotkey v1.1.14.03。

【问题讨论】:

    标签: replace autohotkey


    【解决方案1】:

    对于命令参数,你必须区分变量参数值参数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
    

    【讨论】:

      【解决方案2】:

      使用较新的 StrReplace() 函数,我注意到无论出于何种原因,我都无法将它与变量一起使用。以及这里的文档:https://autohotkey.com/docs/commands/StringReplace.htm 缺少一个例子。经过大量测试,无法弄清楚。 所以我为 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()
      

      另外,请注意,删除数据中的 换行符 比它更难 应该也是。并且一定会在您正在执行的任何字符串解析中使用活动扳手。

      【讨论】:

        【解决方案3】:

        问题在新版本中仍然存在。

        通过表达式的这种替代语法解决:

        newSymbol := StrReplace(Symbol, "" . SearchText, "" . ReplaceText)

        【讨论】:

          猜你喜欢
          • 2012-08-07
          • 2014-08-29
          • 2013-06-05
          • 2011-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多