如果一个字符的重复次数超过阈值值为null或""在vbs?

时间:2017-01-18 16:30:52

标签: string vbscript

Const ForReading = 1
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
strSource = "C:\Users\Desktop\test.csv"
Set File = fso.OpenTextFile(strSource, ForReading)
strText = File.ReadAll
File.Close
strNewText = Replace(strText,",","|")
Set File = fso.OpenTextFile(strSource, ForWriting, True)
File.WriteLine strNewText
File.Close

这是我使用的代码,我得到的结果是: 的 XXXX | YYYY | ZZZZZ ||||||||||||||||||||| 管道人物' |'必须替换为' null'或者''如果重复超过五次,我尝试使用修剪,替换和中间功能,但无法得到解决方案。感谢

2 个答案:

答案 0 :(得分:0)

你试图限制" |"连续5次参赛? 可能有一个更漂亮的方式,但这将起作用

Do While InStr(1, strNewText, "||||||") > 0
    strNewText = Replace(strNewText, "||||||", "|||||")
Loop

答案 1 :(得分:-1)

您可以使用以下regular expression

\|{5,}

如下:

Const ForReading = 1
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
path = "C:\Users\Desktop\test.csv"
Set file = fso.OpenTextFile(path, ForReading)
strText = file.ReadAll
file.Close

Set re = CreateObject("VBScript.RegExp")
re.Global = True
re.Pattern = "\|{5,}"
strNewText = re.Replace(strText, "")

Set file = fso.OpenTextFile(strSource, ForWriting, True)
file.WriteLine strNewText
file.Close

NB。考虑在脚本开头使用Option Explicit;它可以避免拼写错误和混乱的变量。