简体   繁体   English

如何在html字符串上自动添加引号

[英]How to auto add quotation marks on a html string

is it possible to add quotation marks on a html string in order to be userd in visual studio?是否可以在 html 字符串上添加引号以便在 Visual Studio 中使用? For example:例如:

 class="btn btn-success btn-lg" href="

I want it to be我希望它是

"class=""btn btn-success btn-lg"" href="""

so I can use it as string in visual studio.所以我可以在 Visual Studio 中将它用作字符串。 So my question is: Is it possible to create a little tool that, If I give ANY html It will give me back the html string already ready to be used in visual studio?所以我的问题是:是否可以创建一个小工具,如果我提供任何 html,它将返回已经准备好在 Visual Studio 中使用的 html 字符串? Thanks a lot非常感谢

If you really have to, you could write a function like this:如果你真的需要,你可以写一个这样的函数:

Public Function EscapeDoubleQuotations(s As String,
                                       Optional encloseInQuotes As Boolean = True) As String
    Dim escaped As String = s.Replace("""", """""")
    If encloseInQuotes Then escaped = """" & escaped & """"

    Return escaped
End Function

This will replace every double-quotation character in any text (not just HTML) with two double-quotations.这将用两个双引号替换任何文本(不仅仅是 HTML)中的每个双引号字符。 This is particularly useful if you're trying to do this programmatically (eg, to automate some task).如果您尝试以编程方式执行此操作(例如,自动执行某些任务),这将特别有用。

However, in my opinion, it's not worth creating a tool simply to escape the double-quotations.然而,在我看来,仅仅为了逃避双引号而创建一个工具是不值得的。 You can easily do that in any text editor (eg, Notepad) using the "Find and Replace" utility.您可以在任何文本编辑器(例如,记事本)中使用“查找和替换”实用程序轻松完成此操作。 But if you think a tool would make things easier for you, then go ahead.但是,如果您认为某个工具会让您的工作变得更轻松,那么请继续。 You can use the function above to replace some text that is loaded from a file or manually added to a TextBox or something.您可以使用上面的函数来替换从文件加载或手动添加到 TextBox 或其他东西的一些文本。

Not sure how you are adding the string to an HTML element but...不确定如何将字符串添加到 HTML 元素,但是...

    Const bar As String = "class=" & ControlChars.Quote & "btn btn-success btn-lg" & ControlChars.Quote & " href=" & ControlChars.Quote & ControlChars.Quote
    '  produces   class="btn btn-success btn-lg" href=""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM