Hi guys I have an imageUrl with backSlashes('\\') I want to replaceAll the backSlashes with a normal slash ('/')
this is an imageUrl example :
public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg
I want to make it with normal slashes like this one :
public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg
this is my code :
path = decoded['profilePictureUrl'];
if (path!=''){
path.replaceAll('/', '\\');
path=ApiConstants.BASE_URL+path;
print(" my path now : "+path);
I'm getting a wrong result with this function any help would be appreciated
I think you got the 2 parameters swapped.
You have:
path.replaceAll('/', '\\');
It should be:
path.replaceAll('\\', '/');
Unrelated, but instead of if (path!='') you could write if (path.isNotEmpty) for improved readability.
Also, you can use the letter r in front of a String to make it verbatim.
Try this:
final s = r'public\images\providerProfilePictures\2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg';
final url = s.replaceAll('\\', '/');
print(url);
Console output:
flutter:
public/images/providerProfilePictures/2022-04-04T19-08-50.943Z-scaled_image_picker8914175492511161913.jpg
Related
Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.
You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.
var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);
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.
I have a folderPath which has a directory string:
/home/bastian/Pictures
and I have a variable fileName which contains the name.
I can concatenate the two strings together like this, but it only works on UNIX systems:
let filePath = folderPath + '/' + fileName;
Is there a way with GLib I can concatenate the two to each other without making assumptions about the slash or backslash (to stay fx Windows-compatible)?
With help from guadec, I found out I could use GLib's g_build_filenamev () function.
let filePath = GLib.build_filenamev([folderPath, fileName]);
This builds a path to the file and respects the platform at the same time.
Note: it requires that you import GLib first at the top of your GJS file, like this:
const { GLib } = imports.gi;
If you happen to be using a Gio.File object to manipulate the path, you can also do something like this:
const folder = Gio.File.new_for_path(folderPath);
const file = folder.get_child(fileName);
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 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?