用双引号替换字符串中的字符

时间:2015-04-08 07:37:43

标签: regex asp-classic

这是我的字符串:

<HOLDERS><ACCOUNTHOLDER Title="" Initials="" FirstName="" Surname="" Name="AN'A"N&D & TEST'S"I&X" CifKey="ANA"D.TSX000" CustomerType="2" PrimaryPan="00027272898"/></HOLDERS>

如何在名称和cifkey中替换双引号,并用

替换它们
&quot; 

虽然仍然在字符串中的其他地方保持双引号?

输出应为

<HOLDERS><ACCOUNTHOLDER Title="" Initials="" FirstName="" Surname="" Name="AN'A&quot;N&D & TEST'S&quot;I&X" CifKey="ANA&quot;D.TSX000" CustomerType="2" PrimaryPan="00027272898"/></HOLDERS>

1 个答案:

答案 0 :(得分:0)

我写了所有的段落只是为了清楚但你可以创建一个函数并将所有内容减少到几行。

可能不是最干净的方式,但是开始让它工作并在之后改进它。假设txt是你的字符串。

index1 = InStr(txt," Name=") 'space before Name is important not to confuse with firstName, Surname and so on...
index2 = InStr(txt, " CifKey=") 'space before Cifkey is important...
index3 = InStr(txt, " CustomerType=") 'space before CustomerType is important...

SubStrName = Mid(txt, index1 + 7, index2 - 1)
txt = Replace(txt,"SubStrName","##AAA##") 'modifing original string with placeholder which you should be sure is never inside the text
NewChunk1 = Replace(SubStrName,""","&quot;")
txt = Replace(txt,"##AAA##","NewChunk1")

SubStrCifKey = Mid(txt, index2 + 8, index3 - 1)
txt = Replace(txt,"SubStrCifKey","##BBB##")
NewChunk2 = Replace(SubStrName,""","&quot;")
txt = Replace(txt,"##BBB##","NewChunk2")