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
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")
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!
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 '\'
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.
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"
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..