Removing trailing slash in a String - dart

Some users type https://google.com/ and some users type https://google.com
How to check If user type with slash or not? If user type with a slash How to remove slash?

var str = 'https://google.com/';
if (str.endsWith('/')) str = str.substring(0, str.length - 1);

If you re doing lots of string manipulations you can use a sanitize package https://pub.dev/documentation/validators/latest/sanitizers/sanitizers-library.html, in your case it would be rtrim(str, ' /')

Related

Is there any way to replace double quotes with a backslash in swift

I have a submit form where there are multiple textfields.
Whenever user enters text like "Hi, my name is "xyz"", the service does not accept this JSON due to double quotes in my string.
Please suggest ways to escape this character.
I have tried using encode and decode JSON, replaceOccurrencesOf methods, but none work.
replaceOccurrencesOf()
The below code snippet with replace "(double quote) in a string by \". This will help to replace "(double quote) by any string or character in a given string.
Swift 5 or above
let replacedString = stringToBeModified.replacingOccurrences(of: "\"", with: #"\""#)
Instead of putting the name (i.e., "XYZ" if you getting xyz from textfield ) why not to place (textField.text!) it will not put extra " "

#HttpContext.Current.User.Identity.Name not showing backslash

Super Simple. Only issues I find are people getting null. Which I obvi fixed. But where is the backslash???!!
params.me = '#HttpContext.Current.User.Identity.Name';
This returns
"domainUserName" <- Browser
"domain\\UserName" <- Debugging
What I expect is
"domain\UserName" <- Browser
Any ideas?
Based on your comments you are using the following code to show the user name:
alert('#HttpContext.Current.User.Identity.Name');
#HttpContext.Current.User.Identity.Nameis a string that can contain "\" backslash character. This character is considered as a escape character in javascript as it is in C# as well.
You need to escape the "\" character in the string before passing it to Javascript like that:
alert('#HttpContext.Current.User.Identity.Name.Replace("\\", "\\\\")')

namevaluecollection removes "+" characters from querystring

I have the followigurl localhost.dev?q=dyYJDXWoTKjj9Za6Enzg4lB+NHJsrZQehfY1dqbU1fc= and extract the query string as follows:
NameValueCollection query = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query);
string str1 = query[0];
If i call query.ToString() it shows the correct characters query string. However, when I access the value from the NameValueCollection 'query[0]' the "+" is replaced by a empty " " i.e. dyYJDXWoTKjj9Za6Enzg4lB NHJsrZQehfY1dqbU1fc=
I've tried specifing different encoding and using the Get method from the namevaluecollection. I've also tried spliting the string, but the "+" is being removed each time. Has anyone got any ideas? Many thanks
You can't use this chars in the url variables, you need use URLEncode and URLDecode of HttpUtility class to convert this into a valid url.
I hope this help you.

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

Make sure a string starts or ends with another string in Rails

Is there an easy way to apply the following logic to a string in Rails?
if string does NOT end with '.' then add '.'
if string does NOT begin with 'http://' then add 'http://'.
Of course, one might do something like:
string[0..6] == 'http://' ? nil : (string = 'http://' + string)
But it is a little clunky and it would miss the https:// alternative. Is there a nicer Rails way to do this?
Regex's are key, but for a simple static string match, you could use helper functions which are faster. Also, you could use << instead of + which is also faster (though probably not a concern).
string << '.' unless string.end_with?( '.' )
string = 'http://' << string unless string.start_with?( 'http://' )
note, s will contain the correct value, but the return value is nil if the string was not changed. not that you would, but best not to include in a conditional.
Something like
string = "#{string}." unless string.match(/\.$/)
string = "http://#{string}" unless string.match(/^https?:\/\//)
should work.
These will work in 1.9:
s += '.' unless s[-1] == '.'
s = 'http://' + s unless s[/^https?:\/\//]
The first one won't work in 1.8 though.

Resources