VBScript删除换行符

时间:2011-02-17 20:09:55

标签: vbscript

我在</html>之后有一个包含一个或多个换行符的HTML页面。我的VBScript文件能够找到用空虚替换换行符。但是,看起来OpenTextFile再次将换行符放在最后。救命啊!

'Pulled this from the InterWebs
Const ForReading = 1 Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("a.html", ForReading)
strText = objFile.ReadAll
'Wscript.Echo strText
objFile.Close

strNewText = Replace(strText, "</html>" & vbCrlf, "</html>")
Set objFile = objFSO.OpenTextFile("a.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

3 个答案:

答案 0 :(得分:4)

而不是objFile.WriteLine strNewText使用objFile.Write strNewText。这将在最后编写没有换行符的文件。

顺便说一下,在</html>标记之后移除换行符的另一种方式是strNewText = Trim(strText)而不是Replace()

答案 1 :(得分:3)

这可能有所帮助:

TextStream对象具有以下用于写入文本文件的重要方法:

  • Write(string) - 将字符串写入打开的文本文件。
  • WriteLine(string) - 将字符串写入文本文件,并使用换行符完成。
  • WriteBlankLines(lines) - 写入指定数量的新行字符。

如果您不想在最后添加换行符,请使用

objFile.Write strNewText

而不是

objFile.WriteLine strNewText

答案 2 :(得分:2)

您的代码大多是正确的。添加换行符不是OpenTextFile,而是WriteLine。如果用Write替换它,它将按预期工作。

相关问题