New Line Command (\n) Not Working With Firebase Firestore Database Strings
Solution 1
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 string 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!
Solution 2
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")
Solution 3
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.
Solution 4
You can use CSS whitespace property for \n, it works for me.
white-space: pre-line;
Solution 5
Solution: Add this to your string and you are done (for Java users):
.replace("\\n", "\n")
Example:
if (dataSnapshot.exists())
{
ArrayList<String> userlogs2 = new ArrayList<String>();
userlogs2.add(dataSnapshot.getValue().toString().replace("\\n", "\n"));
Iterator<String> it2 = userlogs2.iterator();
while (it2.hasNext()) {
appendColoredText(userlogsmessages2, it2.next() + "\n", Color.BLACK);
appendUnderlinedText(userlogsmessages2,"____________" + "\n\n", Color.parseColor("#DB808080"));
}
![Jacob Cavin](https://i.stack.imgur.com/aLPRJ.jpg?s=256&g=1)
Jacob Cavin
I've self-taught myself iOS development using Swift and Xcode over the past 6 years. I'm now going to school to get an Associate's degree in applications development. I'm proficient in Swift but also know some Java, JavaScript, MySQL, HTML, CSS, Bash, Visual Basic, and PHP. I also ran social media accounts for 5 years, accumulating millions of views. Because of this, I learned about graphic design, photography, videography, and SEO.
Updated on June 03, 2022Comments
-
Jacob Cavin about 2 years
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 strings, I am using the new line command (or\n)
. So some of my strings look like this:"This is line one\nThis is line two\nThis is line three"
But, whenever that string is retrieved, it's addetoto the
UILabel
and appears like this...This is line one\nThis is line two\nThis is line three
...when it should be like this...
This is line one
This is line two
This is line three
I'm assuming that
\n
does not work with strings coming from a database? I've tried double escaping with\\n
. Does anyone have a fix for this?Here is the code that I am using...
database.collection("songs").whereField("storyTitle", isEqualTo: "This is the story header").getDocuments { (snapshot, error) in for document in (snapshot?.documents)! { self.storyBodyLabel.text = (document.data()["storyBody"] as? String)! } }
-
David Knipe over 6 yearsDo you mean "Firebase Realtime Database doesn't support newline characters [and some other characters]"? Database storage and string literals are separate issues.
-
Doug Stevenson over 6 yearsNo, you can put whatever characters you want in Firestore. I even put emoji in there recently. You just can't expect Firestore to understand any sort of character escape sequences that might have meaning in programming languages. twitter.com/CodingDoug/status/961302822997213184
-
Jacob Cavin over 6 years@DougStevenson I'm sorry. I'm fairly new to coding. How would I do this?
-
Doug Stevenson over 6 yearsThat sounds like a different question to ask here on Stack Overflow.
-
Jacob Cavin over 6 years@DougStevenson Ok. I'll look and see if anyone else has asked. I'm sure they have. Thanks for your help! 👍🏻
-
David Knipe over 6 years@DougStevenson 'If you write "\n" in a string, ...' It looks like you meant a string generated from a string literal containing
\n
(i.e. a string containing a newline character, generated from a literal). But now I think you meant a string consisting of the characters` and
n` (which you'd get from the string literal"\\n"
). And you're not saying that Firebase doesn't accept strings generated from string literals containing\n
, you're just saying that Firebase doesn't do any escaping itself. Is this what you meant? Just trying to clarify things. -
Doug Stevenson over 6 yearsFirestore will take the string data exactly as it appears in the string at runtime, as passed to the API. At compile time, there may be a \n visible to the developer in source code, but that may have special meaning to the compiler (encoded newline), and that's not at all visible to Firestore. It'll just see the newline at runtime. If the developer attempts to use \\n to effectively pass the characters \n to Firestore, it will take those characters literally and do no other transformation.
-
Rufat Mirza over 6 yearsAre you sure you are replacing \n? Firebase converts \n into \\n so you need to convert back with stringRecived.replacingOccurrences(of: "\\n", with: "\n")
-
Jorge Vicente Mendoza almost 6 yearsRufat Mirza has the right answer for this issue!, thanks for your comment
-
awaik almost 5 yearsThank you for answer! Now the command is stringRecived.replaceAll(RegExp(r'\\n'), '\n')