Printing and formatting strings with """"

Hi! I have a string that is updated via properties and wanted to ask if this is the only way to control strings with properties because all the “”" are doing my head in.

Here is my example string:
lb_result.Text = "<iframe src="""+Self.source+""" height="""+Self.fheight+""" width="""+Self.fwidth+""" title="""+Self.title+"""></iframe>"

The thing is adding spaces or anything else causes headaches as I end up missing a " here or there and spending too much time trying to figure out which one or where the " needs to go.

Regards,

You can use a constant and String.ReplaceAll with some placeholders. This is what I do when I have some lengthy HTML or JavaScript.

kHTML

<iframe src="%src%" height="%height%" width="%width%" title="%title%"></iframe>

Usage:

var sHTML as String = kHTML
sHTML = sHTML.ReplaceAll("%src%", "https://url")
sHTML = sHTML.ReplaceAll("%height%", "480")
sHTML = sHTML.ReplaceAll("%width%", "640")
sHTML = sHTML.ReplaceAll("%title%", "My Great Xojo App")

lb_result.Text = sHTML

If you need the placeholder swapped value in multiple places create a factory method for it.

5 Likes

Thank you for this. It really made things a lot easier.