如何仅在双引号之间替换分号? [英] How to replace semicolons only when between double quotes?

查看:36
本文介绍了如何仅在双引号之间替换分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 VBScript 我需要在下面的文本行中将分号替换为逗号,但仅限于双引号 Chr(34) 内的子字符串.我尝试使用 Do While 循环,但没有成功.

Using VBScript I need to replace semicolons to commas in the below text line, but only in substrings that are within double quotation marks Chr(34). I tried looping with Do While but didn't suceeded.

2015/003;2015-12-15;;;;EUR;;;"170946;01";"164332;04";;"23937;99";91

我试过的代码:

stop_1 = InStr(1, text, Chr(34))
stop_2 = InStr(InStr(1, text, Chr(34))+1, text, Chr(34))
Do While stop_1 > 0
  text = Replace(Mid(text, stop_1+1, stop_2-stop_1), ";", ",")
  stop_1 = InStr(1, text, Chr(34))
  stop_2 = InStr(InStr(1, text, Chr(34))+1, text, Chr(34))
Loop

推荐答案

考虑下面的例子.此代码将字符串拆分为子字符串数组,在源字符串中由双引号分隔,因此第一个元素是第一个双引号之前的文本,第二个元素是双引号之间的文本,依此类推:奇数元素对应于双引号中的文本,甚至是双引号内的元素.然后从索引为 1 的第二个元素(从零开始的数组)和第 2 步开始循环,因此仅处理双引号内的元素:将 ; 替换为 ,.之后加入处理过的数组以检索字符串.

Consider the below example. This code splits string to array of substrings, that are delimited by double quotes in source string, so the first element is the text before the first double quotes, second element is the text between double quotes, and so on: odd elements correspond to the text out of double quotes, and even elements - within double quotes. Then loops starting from second element with index 1 (zero-based array) and step 2, thus processing the elements within double quotes only: replaces ; with ,. After that joins processed array to retrieve the string.

s = "2015/003;2015-12-15;;;;EUR;;;""170946;01"";""164332;04"";;""23937;99"";91"
a = Split(s, """")
For i = 1 To UBound(a) Step 2
    a(i) = Replace(a(i), ";", ",")
Next
s = Join(a, """")

这篇关于如何仅在双引号之间替换分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆