【问题标题】:string replace vb.net not working字符串替换 vb.net 不起作用
【发布时间】:2011-05-26 08:16:36
【问题描述】:

我在这里做错了什么?

If FilePath.ToLower().Contains(".pdf") Then
    Dim Replaced As String = FilePath.Replace("\","/")
    FilePath = "http:" & Replaced
End If 

例如,如果 FilePath 为 \\sharepoint\file.pdf,则预期输出应为 http://sharepoint/file.pdf。但是实际输出的是http:\\sharepoint\file.pdf

更新 1

这是原始字符串:

这是我的 VB 代码后的样子:

如您所见,添加了 http: 部分,但没有触及反斜杠。

更新 2 它与斜线有关。因为当我替换其他字符(例如用@ 替换)时,替换的字符串会正确显示。但不是斜线

【问题讨论】:

  • 确定 FilePath 是否包含:\\sharepoint\file.pdf 开头?您如何测试输入/输出值?
  • 我尝试了您的代码,但无法复制。请检查:imgur.com/09Iwq您是否按照描述定义了 FilePath?
  • @forsvarir,@Alex,请参阅更新后的问题。我添加了图片,以便您可以看到它确实改变了 something
  • @jao 此代码和相关问题中的代码肯定都有效。您的错误在其他地方,并且完全不相关。我假设您正在丢弃介于两者之间的结果值。

标签: vb.net string


【解决方案1】:

我仍然不完全明白为什么,但以下修复了我的代码:

Dim Replaced As String = FilePath
If FilePath.ToLower().Contains(".pdf") Then
    Replaced = FilePath.Replace("\","/")
    Replaced = "http:" & Replaced
End If 

然后在我使用的vbscript代码中

Sub toonDocument()
dim spobject
set spobject = CreateObject("Sharepoint.Document")
spobject.FilePath = "<% = Replaced %>"
spobject.openen()
set spobject = nothing

所以&lt;% = Replaced %&gt;(而不是&lt;%= FilePath %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2016-02-15
    • 2016-10-05
    • 2012-07-08
    • 2011-08-12
    • 1970-01-01
    相关资源
    最近更新 更多