using Dart How to remove space at the beginning and end of a string?
String name =" Well Wisher ";
or this
String name =" Well Wisher";
or from this
String name ="Well Wisher ";
Thanks #https://stackoverflow.com/users/5882307/omi-shah
the .trim() function is used to remove white space from the front and end of a string
String name =" Well Wisher ";
name.trim(); // "Well Wisher"
I cant get this to work it doesnt replace all the special character and white spaces?
String sentence = "!##$%ˆ&*()<>?:"{} Hello ";
String newSentence = sentence.replaceAll("\\W+","").toLowerCase();
print(newSentence);
Use
String sentense = '!#resu##me'.replaceAll(new RegExp('\\W+'), '');
print(sentense);
Output:
resume
For more reference https://api.dart.dev/stable/2.8.3/dart-core/String/replaceAll.html
You should try this:
String sentence = "!##\$%ˆ&*()<>?:\"{} Hello ";
String newSentence = sentence.replaceAll(RegExp(r'[^a-zA-Z0-9 ]'),"").toLowerCase();
print(newSentence);
I can see that Flutter allows me to use "\n\n" in a string and it causes a line break to appear in a Text item:
final String answer = "This is my text.\n\n"
"Here is the 2nd line.";
This is my text.
Here is the 2nd line.
However, when I try to use content pulled from firebase, and set in a variable, the line break ("\n") is actually printed:
final String answer = faq['answer'];
Shows:
This is my text.\n\nHere is the 2nd line.
How can I get my "\n\n" to actually show up as line breaks?
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.
So you can try something like this:
final String answer = (faq['answer'] as String).replaceAll("\\n", "\n");
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)!
}
}
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!
You can use CSS whitespace property for \n, it works for me.
white-space: pre-line;
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"));
}
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.
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")
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 '\'
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"
100% Working
DB.collection(Global.DB_COLLECTION_PATH).document(NEWS_ID).get().addOnCompleteListener(new OnCompleteListener< DocumentSnapshot >() {
#Override
public void onComplete(#NonNull Task< DocumentSnapshot > task) {
if (task.isSuccessful()) {
body1 = task.getResult().getString("newsBody").replace("\n", "\n");
nBody.setText(body1);
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.e("DB FAILED", e.getMessage());
}
});
I hope it's work for you
I noticed that when you add a string with \n to the JSON export and then upload the JSON to the Realtime Database, it acts as a newline. But if you edit that from the firebase console, it gives you back the string with \n and not a newline.
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..
I am printing code to a TextMesh, and it is always changing. I need to save the string to a PlayerPref, so that I can load it back up. In order to get the text to fit properly, I have to force a line break in the string, using \n.
string text = "line1\nline2";
PlayerPref.SetString("Text", text);
LocationSide1.GetComponent<TextMesh>().text = PlayerPrefs.GetString("Text");
If I set the text without the PlayerPref it will do a line break:
line1
line2
With the PlayerPref it prints as one line:
line1\nline2
Any ideas on how to fix this?
Seems like the '\' is escaped.
Have you tried to unescape the string ?
using System.Text.RegularExpressions;
[...]
string text = "line1\nline2";
PlayerPref.SetString("Text", text);
LocationSide1.GetComponent<TextMesh>().text = Regex.Unescape(PlayerPrefs.GetString("Text"));
myString = Regex.Unescape(myString);