Android Question Split a long string into two or more lines

Massy

Member
Licensed User
Longtime User
Hi, I am trying to split a very long line of code into many lines but with no success...
I have a string variable that is very large as it has an html code inside... so I'm trying to have it in many lines and not in just one line but I can't find how to do it...

This is an example of my code:

Dim Html As String

Html = "<p align=""center""><font color=""#3D1565"" size=""3"" face=""Verdana, Arial, Helvetica, sans-serif"">HELLO WORLD <br />"

and I need to split it in two lines... something like this:

Html = "<p align=""center""><font color=""#3D1565""
size=""3"" face=""Verdana, Arial, Helvetica, sans-serif"">HELLO WORLD <br />"

How should I do it?
Thanks for any help
Massy
 

NJDude

Expert
Licensed User
Longtime User
Instead of having that long HTML line, save it as a file and do this:
B4X:
Dim Html As String 

Html = File.ReadString(File.DirAssets, "myHtml.html")
However, if you insist on splitting that line then you can do this:
B4X:
Html = "<p align=""center""><font color=""#3D1565""" & _ 
"size=""3"" face=""Verdana, Arial, Helvetica, sans-serif"">HELLO WORLD <br />"
But that's too cumbersome.

Also, use single quotes as delimiters for your HTML code, it's easier to handle, for example:
B4X:
Html = "<p align='center'><font color='#3D1565'" & _ 
"size='3' face='Verdana, Arial, Helvetica, sans-serif'>HELLO WORLD <br />"
 
Last edited:
Upvote 0

Massy

Member
Licensed User
Longtime User
I have a template and in some parts I have to put my variables... something like:

<td><div align='center'>Percentage =" & MyVariable & "</div></td>

but it's just few days I'm coding with b4a and really don't know how else to achieve it...
my idea is to first create all the html code in a variable and then save it as a html file and then load it in a webview...
I'm sure there must be a better way to go... but don't know where to find it...
if you have any input I will be all ears.... ;)
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I'd suggest using a StringBuilder.

B4X:
Dim Html As StringBuilder
Html.initialize
Html.Append("<div>A hardcoded string</div>")
Html.Append("<div>The variable value is: '")
Html.Append(MyVariable)
Html.Append("'.</div>")

Dim MyString As String="to the "
Html.Append("<div>You can chain calls ").Append(MyString).Append("Append method</div>")

' the StringBuilder .ToString method returns your 'built' String.
Log(Html.ToString)
' load it into a WebView see http://www.b4x.com/android/help/views.html#webview_loadhtml
MyWebView.LoadHtml(Html.ToString)

Martin.
 
Last edited:
Upvote 0
Top