I'm receiving a JSON response and I need to escape quotation marks.
This is The string I get:
"sign":0,"text":"Continue onto William Elton \"Brownie\" Brown Freeway, I 580"
Now, I need to get the string looking like this:
\"sign\":0,\"text\":\"Continue onto William Elton \"Brownie\" Brown Freeway, I 580\"
But, when I call this method
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
What I get is:
\"sign\":0,\"text\":\"Continue onto William Elton **\\"Brownie\\"** Brown Freeway, I 580\"
So the problem is with the bold part, Brownie, that already had quotation marks, before I started replacing them. How can I solve this, and only escape " and not \" as well?
To do what you asked for, first un-escape the escaped quotes:
Use stringByReplacingOccurrencesOfString to change all the \" into "
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\\\"" withString:#"\""];
Then use stringByReplacingOccurrencesOfString to change all the " into \"
sectionString = [sectionString stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
Related
I'm trying to replace a string that holds double backslash to a string with only single backslash, for example:
\\This\\Is\\Not\\Working
To:
\This\Is\Not\Working
Using:
str = [str stringByReplacingOccurrencesOfString:#"\\\\" withString#"\\"];
But for some reason, The string remains the same (with the double backslash) every single time. What am i doing wrong here?
Sadly, I misinterpreted the console log output. the string was fine, the debugger just showed the single slash as a doubled one. (For escaping purposes i'd imagine).
The below lines of code is fine:
NSString *str=#"\\This\\Is\\Not\\Working";
str = [str stringByReplacingOccurrencesOfString:#"\\\\" withString#"\\"];
Just check value by NSLog or by printing, because in debug console slash is represented as double slash.
Check image for more clear understanding:
NSString *Str = #"\\This\\Is\\Not\\Working";
NSLog(#"%#",Str);// print:-\This\Is\Not\Working
Str = [Str stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"];// in this no replace occurres
NSLog(#"%#",Str); // print:-\This\Is\Not\Working
NSString *Str1 = #"\\\\This\\\\Is\\\\Not\\\\Working";
NSLog(#"%#",Str1);// print:-\\This\\Is\\Not\\Working
Str1 = [Str1 stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"];
NSLog(#"%#",Str1);// print:-\This\Is\Not\Working
Try this:
NSString *str = #"\\This\\Is\\Not\\Working";
str = [str stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
NSLog(#"%#", [str stringByReplacingOccurrencesOfString:#"\\\\" withString:#"\\"]);
1st Line is your user input.
2nd Line converts the double back-slashed user input string into four back-slashed string
3rd line simply replaces four back slashes with two back slashes which results in printing single back slash
I'm facing a problem when I try to remove a character in a 'NSString'. The character is a backend (\n).
My 'NSString' is for example like this :
My text is
also in a second line
And I want to get all in one line like this :
My text is also in a second line
The problem is I don't know how to change this...
I tried to locate the '\n' characters with a loop :
for (int delete = 0; delete < myString.length; delete++)
{
if ([myString characterAtIndex:delete] == 10)
{
[myString stringByReplacingCharactersInRange:NSMakeRange(delete,0) withString:#" "];
}
}
Or things like :
myString = [myString stringByReplacingOccurrencesOfString:#"\r" withString:#" "];
(I see that \r could be the backend in a nslog...)
Nothings work..
Thank you for your help in advance !
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#""];
is correct.
If it doesn't work, then the assumption that there is a combination of "\" and "n" characters is wrong.
Do not use NSLog. NSLog already applies carriage returns to the string. Instead put a breakpoint on the line where we call stringByReplacing... and then hover over the myString. Wait a second or two and you will see the "original unformatted content"...this way you can check what you are really trying to replace..
I am reading a line of code in from a source file on disk and the line is a string, and it is of a string that contains HTML code in it:
line = #"format = #"<td width=\"%#\">";"
I need to remove the escaped characters from the html string. So any place that there is a '\"', I need to replace it with ''. I tried this:
[line stringByReplacingOccurrencesOfString:#"\\""" withString:#""];
But it only removed the '\' character, not the accompanying '"'. How can I remove the escaped '"' from this string?
EDIT: The key part of this problem is that I need to figure out a way to identify the location of the first #", and the closing " of the string declaration, and ignore/remove everything else. If there is a better way to accomplish this I am all ears.
[s stringByReplacingOccurrencesOfString:#"\\\"" withString:#""]
The replacement string there is a slash, which has to be escaped in the literal replacement string using another slash, followed by a quote, which also has to be escaped in the literal by a slash.
Try use this:
NSString *unfilteredString = #"!##$%^&*()_+|abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet];
NSString *resultString = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars] componentsJoinedByString:#""];
NSLog (#"Result: %#", resultString);
http://php.weenggs.com/scrap/service.php?op=search&search_term=Crabbie's ->my api
http://php.weenggs.com/scrap/service.php?op=search&search_term=Crabbie%20 ->want like this
[myStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
is convert blank space to %20 same way I want to convert 's to %20.
try like this you can use stringByReplacingOccurrencesOfString method,
here it replaces occurence of 's with %20
str=[str stringByReplacingOccurrencesOfString:#"'s" withString: #"%20"];
NSString *str=#"http://php.weenggs.com/scrap/service.php?op=search&search_term=Crabbie's";
str=[str stringByReplacingOccurrencesOfString:#"'s" withString: #"%20"];
NSLog(#"%#",str);
O/P:-
http://php.weenggs.com/scrap/service.php?op=search&search_term=Crabbie%20
I have a NSString like this:
Hello
World
of
Twitter
Lets See this
>
I want to transform it to:
Hello World of Twitter Lets See this >
How can I do this? I'm using Objective-C on an iPhone.
Split the string into components and join them by space:
NSString *newString = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#" "];
Splitting the string into components and rejoining them is a very long-winded way to do this. I too use the same method Paul mentioned. You can replace any string occurrences. Further to what Paul said you can replace new line characters with spaces like this:
myString = [myString stringByReplacingOccurrencesOfString:#"\n" withString:#" "];
I'm using
[...]
myString = [myString stringByReplacingOccurrencesOfString:#"\n\n" withString:#"\n"];
[...]
/Paul
My case also contains \r, including \n, [NSCharacterSet newlineCharacterSet] does not work, instead, by using
htmlContent = [htmlContent stringByReplacingOccurrencesOfString:#"[\r\n]"
withString:#""
options:NSRegularExpressionSearch
range:NSMakeRange(0, htmlContent.length)];
solved my problem.
Btw, \\s will remove all white spaces, which is not expected.
Providing a Swift 3.0 version of #hallski 's answer here:
self.content = self.content.components(separatedBy: CharacterSet.newlines).joined(separator: " ")
Providing a Swift 3.0 version of #Kjuly 's answer here (Note it replaces any number of new lines with just one \n. I would prefer to not use regular express if someone can point me a better way):
self.content = self.content.replacingOccurrences(of: "[\r\\n]+", with: "\n", options: .regularExpression, range: Range(uncheckedBounds: (lower: self.content.startIndex, upper: self.content.endIndex)));