如果字符串有空格,如何添加左引号和右引号 [英] How to add open and close quotes if A string has spaces
问题描述
我正在尝试编写一个 If then 语句来查看字符串中是否有空格.如果是这样,我希望它在变量周围放置一个和".以下是我当前的代码:
I'm trying to write an If then statement to see if a string has a space in it. If it does, I want it to put an " and " around the variable. Below is my current code:
If ColumnText.Contains(" ") Then
MsgBox(""" & ColumnText & """)
End If
Next
但它看到引用所有内容...有什么建议吗?
But it's seeing quoting everything... Any suggestions?
推荐答案
要在 VB 的双引号字符串文字中正确转义双引号,您需要将其加倍(无意双关语).这意味着一个空字符串 ""
.当你在其中挤压一个引号时,你会得到 4 个引号 """"
,这实际上意味着只有一个双引号文字.
To properly escape a double quote inside VB's double quoted string literal, you need to double it (no pun intended). This means an empty string ""
. When you squeeze a quote in it, you get 4 quotes """"
, and this really means just one double quote literal.
您应该使用:
MsgBox("""" & ColumnText & """")
代替:
MsgBox(""" & ColumnText & """)
另一件事 - 请注意语法解析器在有 3 个引号时如何突出显示您的行.在这种情况下 &ColumnText &
是文字的一部分,而不是内联变量.
Another thing - notice how the syntax parser highlights your line when it has 3 quotes. In this case & ColumnText &
is part of the literal, instead of being an inline variable.
参考:
这篇关于如果字符串有空格,如何添加左引号和右引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!