vba 在 QTP 中使用 Split 函数从字符串中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20800643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get values from a string using Split function in QTP
提问by narayanan
I need to separate the values from the following string based on delimiter "," in QTP (CurrentAmount=140,000.00, currency=USD, originalAmount=200,000.00)
我需要根据 QTP 中的分隔符“,”将值从以下字符串中分离出来(CurrentAmount=140,000.00,currency=USD,originalAmount=200,000.00)
I used following code below
我在下面使用了以下代码
splittext= Split (strng, ",")
For i=0 to ubound(splittext)
msgbox splittext(i)
next
Problem here is, output value is something like this
这里的问题是,输出值是这样的
0= CurrentAmount=140
1= 000.00
2= currency=USD.... so on
The code split the following value (CurrentAmount=140,000.00,) into two parts because of the "," in the 140,000.00
由于 140,000.00 中的“,”,代码将以下值 (CurrentAmount=140,000.00,) 分为两部分
I need a code to separate the value based on delimiter "," but should ignore the "," present in the numeric data
我需要一个代码来根据分隔符“,”来分隔值,但应该忽略数字数据中存在的“,”
Please let me with this issue... Thanks
请让我解决这个问题...谢谢
回答by Pankaj Jaju
This should be simple - assuming that there is a space after the required delim comma char. Notice the space after comma used in Split
function.
这应该很简单 - 假设在所需的 delim 逗号字符后面有一个空格。注意Split
函数中逗号后的空格。
Public Sub Test()
splitText = Split("CurrentAmount=140,000.00, currency=USD, originalAmount=200,000.00", ", ")
For i = 0 To UBound(splitText)
MsgBox splitText(i)
Next
End Sub
回答by David Knipe
Pankaj Jaju's solution works, assuming you can trust the CSV string to always have spaces after the commas that separate fields. But I think the underlying problem is that the format of the input isn't very clearly specified. If you can change the format, you should get rid of the commas inside the numbers. Alternatively you could escape the commas properly, but then it would be slightly trickier to get the regex right. (And there are varying standards on how to escape commas in CSV format - some systems use \,
, others use quotation marks.)
Pankaj Jaju 的解决方案有效,假设您可以相信 CSV 字符串在分隔字段的逗号之后始终有空格。但我认为潜在的问题是输入的格式不是很清楚。如果可以更改格式,则应该去掉数字中的逗号。或者,您可以正确地转义逗号,但是要正确使用正则表达式会稍微棘手一些。(关于如何以 CSV 格式转义逗号有不同的标准 - 有些系统使用\,
,其他系统使用引号。)