【问题标题】:How to replace string in MUMPS where new string consist of old string?如何在新字符串由旧字符串组成的 MUMPS 中替换字符串?
【发布时间】:2022-10-15 02:08:43
【问题描述】:

试图在 MUMPS 中编写代码以在字符串中用新的替换旧的。 然而新包含旧。

预期答案="我的老朋友" 但是得到了=“我的老朋友” 我如何得到预期的答案?

s str="My old old friend"
s old="old"
s new="very old" 
n ctr,max
s max=$L(str)
f ctr=1:1:($L(str,$E(old))-1) i $F(str,old)>0 s $E(str,$F(str,old)-$L(old),$F(str,old)-1)=new s count=count+1
q str

【问题讨论】:

    标签: mumps


    【解决方案1】:

    我认为您遇到的问题是您正在就地修改 str 。所以第一个“旧”被替换为“非常旧”。然后 $F 从“非常旧”中看到“旧”并替换它。

    1. 我的老朋友。 <-- 第一个“旧”被替换
    2. 我的老朋友<--那么“老”中的“老”就是 更换。
    3. 我的老朋友

      您最好创建一个新字符串以返回,而不是就地修改。这是我想出的:

      S STR="My old old friend"
      S OLD="old"
      S NEW="very old"
      F I=1:1:$L(STR," ") S TOK=$P(STR," ",I) S:TOK=OLD TOK=NEW S OSTR=$G(OSTR)_" "_TOK
      W OSTR,!
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      相关资源
      最近更新 更多