How to deal with dynamic urls with special characters like single quote? - encode

I am generating dynamic an "a href" html tag on my asp page. Also the url is dynamic. Sometimes there are special characters inside the url and the hyperlink is not working. For example when there is an single quote:
http://myCompany.com/'s-hertog.aspx
How can I fix this that the dynamic url always will work?
I already try this, but is not working:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href='{0}' >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);

I found the solution by my self. I changed the single quotes to double quotes in the string.format:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href=\"{0}\" >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);

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

Parse a string with specific condition

How can I trim my string which is in this form:
https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
to this?
media/93939393hhs8.jpeg
I want to remove all the characters before the second to last slash /.
I can use stringByTrimmingCharactersInSet but I don't know how to specify the condition that I want:
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet() // what here in my case ??
)
The above is for removing the white spaces, but that is not the case here.
Since the string is an URL get the path components, remove anything but the last 2 items and join the items with the slash separator.
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString)
}
You're not trimming, you're parsing.
There's no single call that will do what you want. I suggest writing a block of code that uses componentsSeparatedByString("\n") to break it into lines (one URL per line), then parse each line separately.
You could use componentsSeparatedByString("/") on each line to break it into the fragments between your slashes, and then assemble the last 2 fragments together.
(I'm deliberately not writing out the code for you. You should do that for yourself. I'm just pointing you in the right direction.)
You might also be able to use NSURLComponents to treat each line as a URL, but I'm not sure how you'd get the last part of URL before the filename (e.g. "media " or "lego") with that method.

#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.

NSString to NSDictionary

I have a string (from HTTP Header) and want to split it into a dictionary.
foo = \"bar\",baz=\"fooz\", beta= \"gamma\"
I ca not guarantee that the string is the same every time. Maybe there are spaces, maybe not, sometimes the double quotes are escaped, sometimes not.
So I found the solution in PHP with regular expressions. Unfortunately I can't convert it to work on iOS.
preg_match_all('#('.$key.')=(?:([\'"])([^\2]+?)\2|([^\s,]+))#', $input, $hits, PREG_SET_ORDER);
foreach ($hits as $hit) {
$data[hit[1]] = $hit[3] ? $hit[3] : $hit[4];
}
Can anybody help me converting this to Objective-C?
I met a guy which is kinda RegEx guru. He explained the whole stuff and I got the following (working!!!!) solution in RegEx.
This gives me strings like foo="bar":
(?<=[,\\s])((realm|qop|nonce|opaque)=(?:([\"'])([^\2]+?)\2|([^\\s,]+)))
I then use another RegEx to split it by key and value to create a dictionary.

Resources