New Line Command (\n) Not Working With Firebase Firestore Database Strings

后端 未结 6 993
天涯浪人
天涯浪人 2020-12-06 01:36

I\'m making an app with Swift and I\'m using Firebase Firestore. Firestore is a database that has some strings that I put into a UILabel. With some of my stings

相关标签:
6条回答
  • 2020-12-06 02:12

    Tried all of the answers suggested but none worked for me. In the end, I fixed it by using "/n" in the Firestore record and, in the Swift client, the following:

    label.text = stringReceived.replacingOccurrences(of: "/n", with: "\n")
    
    0 讨论(0)
  • 2020-12-06 02:18

    I got it. I simply just replaced the character "\n" from the string that I was receiving with the newline command.

    label.text = stringRecived.replacingOccurrences(of: "\n", with: "\n")
    

    Because I manually typed out my string and gave Firebase a sting like "Line one\nline two\nline three" I am replacing "\n" with "\n" But if you give Firebase a string like

    "Line one
    Line two
    Line three"
    

    Firebase replaces those returns with "\\n" therfore making the code

    label.text = stringRecived.replacingOccurrences(of: "\\n", with: "\n")
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-06 02:19

    For me firebase turned all \n to \\\\n , so I just reversed that change with :

    theString.replaceAll( "\\\\n", "\n" );
    

    Just posting cause I wasted some time calculating the right number of '\'

    0 讨论(0)
  • 2020-12-06 02:21

    Firestore doesn't support any escape sequences within string values. If you write "\n" in a string, you're going to get exactly that back when you read it. If you need to store something special, you may want to encode and decode that yourself.

    0 讨论(0)
  • 2020-12-06 02:34

    I found I could get newlines into a firestore field by using the String.fromCharCode() function. Firestore seems to need special characters in strings to be the actual character ASCII values and does not reinterpret escape characters.

    i.e.

    "First Line " + String.fromCharCode(13) + "second line"

    0 讨论(0)
  • 2020-12-06 02:35

    Firestore add this type----Line one\nline two\nline three

    get Sting, replace and show with TextView

    String sura=modellist.get(position).getSura().replace( "\n", "\n");

    Working..

    0 讨论(0)
提交回复
热议问题