How do I maintain a string linebreak through a PlayerPref? - unity3d

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);

Related

Remove Special Character from String in API

I have an API and it response me a string, that string contains a URLs of photos,
but the URLs contains special characters (backslash),
how can I remove that Special Character from String in API?
this is the String :
"photos":
"[\"uploads\\/products\\/photos\\/9UZGRucASnXoKsn6fnLqLcWYS7Ttb84JPOHOyJR1.jpeg\",\"uploads\\/products\\/photos\\/zjofjxnsjWus210f5S3YuijJMt9wXSnI6frgNRXc.jpeg\",\"uploads\\/products\\/photos\\/BA5oMG3EPVSDmQcLTYH3DZj1igwg4gMUvC6ItxmT.jpeg\",\"uploads\\/products\\/photos\\/gcCyQzli8M4ZGIWfcuU7DKf9C2y8rVxq5OpSHT9w.jpeg\",\"uploads\\/products\\/photos\\/1xPnp6ut6C7wJUlGHqUlnGrL6H3ClIPs0eFM96yc.jpeg\",\"uploads\\/products\\/photos\\/JjlVTcv6YencrYQClxCL2FRzMD6DUelfggtHVHbI.jpeg\",\"uploads\\/products\\/photos\\/b1iroJ8YWMj9PsTmSHML1iRlX3G8ayjYvhNP3bZO.jpeg\",\"uploads\\/products\\/photos\\/jUo1bUWi5bjEkk4c9WyQgGhUNGjqDYuFScildcq2.jpeg\"]",
When you have a string, you can pass in the url of the photo path in this case to a variable. Then you can use RegExp to replace all occurrences of a certain character in a String.
final myString = 'abc=';
final withoutEquals = myString.replaceAll(RegExp('='), ''); // abc
In your case:
String photoUrl = //Your photo Url
final cutPhotoUrl = photoUrl.replaceAll(RegExp('\'), ''); // Replaces all occurrences of backslash with blank whitespace
The idea really is to use the replaceAll method, but you must escape the backslash as it follows:
string.replaceAll(RegExp(r'\\'), '')
If you don't do so, you are actually escaping your last ` character and will get an error.

Flutter dart replaceAll special character not working

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);

Flutter \n\n not breaking lines unless hard-coded string

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");

Flutter Unicode Apostrophe In String

I'm hoping this is an easy question, and that I'm just not seeing the forest due to all the trees.
I have a string in flutter than came from a REST API that looks like this:
"What\u0027s this?"
The \u is causing a problem.
I can't do a string.replaceAll("\", "\") on it as the single slash means it's looking for a character after it, which is not what I need.
I tried doing a string.replaceAll(String.fromCharCode(0x92), "") to remove it - That didn't work.
I then tried using a regex to remove it like string.replaceAll("/(?:\)/", "") and the same single slash remains.
So, the question is how to remove that single slash, so I can add in a double slash, or replace it with a double slash?
Cheers
Jase
I found the issue. I was looking for hex 92 (0x92) and it should have been decimal 92.
I ended up solving the issue like this...
String removeUnicodeApostrophes(String strInput) {
// First remove the single slash.
String strModified = strInput.replaceAll(String.fromCharCode(92), "");
// Now, we can replace the rest of the unicode with a proper apostrophe.
return strModified.replaceAll("u0027", "\'");
}
When the string is read, I assume what's happening is that it's being interpreted as literal rather than as what it should be (code points) i.e. each character of \0027 is a separate character. You may actually be able to fix this depending on how you access the API - see the dart convert library. If you use utf8.decode on the raw data you may be able to avoid this entire problem.
However, if that's not an option there's an easy enough solution for you.
What's happening when you're writing out your regex or replace is that you're not escaping the backslash, so it's essentially becoming nothing. If you use a double slash, that solve the problem as it escapes the escape character. "\\" => "\".
The other option is to use a raw string like r"\" which ignores the escape character.
Paste this into https://dartpad.dartlang.org:
String withapostraphe = "What\u0027s this?";
String withapostraphe1 = withapostraphe.replaceAll('\u0027', '');
String withapostraphe2 = withapostraphe.replaceAll(String.fromCharCode(0x27), '');
print("Original encoded properly: $withapostraphe");
print("Replaced with nothing: $withapostraphe1");
print("Using char code for ': $withapostraphe2");
String unicodeNotDecoded = "What\\u0027s this?";
String unicodeWithApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '\'');
String unicodeNoApostraphe = unicodeNotDecoded.replaceAll('\\u0027', '');
String unicodeRaw = unicodeNotDecoded.replaceAll(r"\u0027", "'");
print("Data as read with escaped unicode: $unicodeNotDecoded");
print("Data replaced with apostraphe: $unicodeWithApostraphe");
print("Data replaced with nothing: $unicodeNoApostraphe");
print("Data replaced using raw string: $unicodeRaw");
To see the result:
Original encoded properly: What's this?
Replaced with nothing: Whats this?
Using char code for ': Whats this?
Data as read with escaped unicode: What\u0027s this?
Data replaced with apostraphe: What's this?
Data replaced with nothing: Whats this?
Data replaced using raw string: What's this?

Can't unescape escaped string with ABAP

I want to escape this string in SAPUI5 like this.
var escapedLongText = escape(unescapedLongText);
String (UTF-8 quote, space, Unicode quote)
" “
Escaped string
%22%20%u201C
I want to unescape it with this method, but it returns empty. Any ideas?
DATA: LV_STRING TYPE STRING.
LV_STRING = '%22%20%u201C'.
CALL METHOD CL_HTTP_UTILITY=>UNESCAPE_URL
EXPORTING
ESCAPED = LV_STRING
RECEIVING
UNESCAPED = LV_STRING.
I changed the code in SAPUI5 to the following:
var escapedLongText = encodeURI(unescapedLongText);
This results in: (like andreas mentioned)
%22%20%e2%80%9c
If I want to decode it later in SAPUI5, it can be done like this:
var unescapedLongText = unescape(decodeURI(escapedLongText));
The unescape needs to be done, because commas (for example) don't seem to be decoded automatically.