Android Question Saving Default String Data Containing CRLF

RichardN

Well-Known Member
Licensed User
Longtime User
Since before KVS was born I have saved default field data by assigning each field to a List element then saving to file with File.WriteList()

Apparently that does not work when the string data contains CRLF because the line are divided up and added as individual list elements. So I gave KVS a try only to discover exactly the same thing happens. I also tried String.Replace(CRLF,"|") to change the CRLF into a handy substitute to decode on retrieval but that does not work either. (Maybe I'm not hitting the right string element?)

Is there a quick and easy way to save default string values containing CRLF without it's own dedicated file for read/write?
 

agraham

Expert
Licensed User
Longtime User
I also tried String.Replace(CRLF,"|") to change the CRLF into a handy substitute to decode on retrieval
That should work. Have you overlooked that Strings are immutable and String.Replace needs to be assigned?
B4X:
Dim s as String

s = "fred " & CRLF & "Jim"
s = s.Replace(CRLF, "|")
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
@agraham... Yes I have used that method extensively for reading and writing text to Sqlite. I'm all done banging my head against the wall for today. Will take another look tomorrow.
 
Upvote 0

emexes

Expert
Licensed User
Also CRLF is really just LF, so if your string actually contains Chr$(13) + Chr$(10) then after you String.Replace(CRLF,"|") it will instead contain Chr$(13) + "|"

That sounded much simpler in my head before I wrote it down. šŸ™ƒ
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Too much screen time yesterday!

KVS works fine after some familiarisation. The KVS library situation is rather confusing though!....

KVS2 is deprecated whilst KVS is the most up to date ????? A situation made much worse by the fact that the comments in the library panel only become visible once the library is loaded so are easily missed. When loading a library you scroll down to it..... check the box and it immediately moves out of sight to the top of the list so the comments are not immediately evident.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
Hi guys,
CRLF didn't work!
Today I was working on Andrew Graham's PageTurner example app, I tried to modify (page 0) to make it with 3 lines instead of 1 line. I didn't work..
even though I used CRLF but it shows a long string, concacnated string in 1 line.
Any help apprechated!

PageTurner page#0:
Sub PageTurner_GetBitmap(Width As Int, Height As Int, Page As Int) As Bitmap
    Dim bmp As Bitmap
    Dim can As Canvas
     ' do this here so we have a valid return in case of an exception
    bmp.InitializeMutable(Width, Height)
    ' if on page 0 create the "Start" page
    If Page=0 Then
        can.Initialize2(bmp) : can.DrawColor(Colors.DarkGray)
        can.DrawText("Turn Page for Help", Width/2, Height/3, Typeface.DEFAULT, 24, Colors.White, "CENTER")
        Return bmp
    End If
    ' if past the end of the pages create the "End" page
    If Page>NUMPAGES Then
        can.Initialize2(bmp) : can.DrawColor(Colors.DarkGray)
        can.DrawText( "End", Width/2, Height/3, Typeface.DEFAULT, 24, Colors.White, "CENTER")
        Return bmp   
    End If
    ' load the image for the page
    Try   
        can.Initialize2(bmp) : can.DrawColor(Colors.Black)
        Dim thisBitmap As Bitmap = LoadBitmap(File.DirAssets, Page & ".png")
        Dim dst As Rect : dst.Initialize(0,0,thisBitmap.Width,thisBitmap.Height)
        ' scale the image to fit on the page
        Dim aspect As Float = thisBitmap.Width/thisBitmap.Height
        If dst.Right>Width Or dst.Bottom<Height Then
            dst.Right=Width : dst.Bottom=Floor(dst.Right/aspect)
        End If
        If dst.Bottom>Height Or dst.Right<Width Then
            dst.Bottom=Height : dst.Right=Floor(dst.Bottom*aspect)
        End If
        can.DrawBitmap(thisBitmap,Null,dst)
        Return bmp
    Catch
        ' catch and report any exceptions on the rendering thread to the main thread
        PTException
    End Try   
    ' return something to avoid causing an exception on the rendering thread
    Return bmp 
End Sub
 
Upvote 0
Top