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
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 have a NSString with parentheses in it.
I would like to remove the Text inside of the parentheses.
How to do that? ( In Objective-C )
Example String:
Tach auch. (lockeres Ruhrdeutsch) Und Hallo!
I would like to Remove "(lockeres Ruhrdeutsch)" from the String,
but the Strings i have to edit are always different.
How can i remove the String betweeen "(" and ")"?
Best Regards
Use regular expression:
NSString *string = #"Tach auch. (lockeres Ruhrdeutsch) Und Hallo!";
NSString *filteredString = [string stringByReplacingOccurrencesOfString:#"\\(.*\\)"
withString:#""
options:NSRegularExpressionSearch range:NSMakeRange(0, string.length)];
NSLog(#"%#", filteredString);
If you want to consider also a whitespace character after the closing parenthesis, add \\s? to the end of the regex pattern.
Here is the function you can call to get your required string:
-(NSString*)getStringWithBlankParaFrom:(NSString*)oldStr{
NSArray*strArray1=[oldStr componentsSeparatedByString:#"("];
NSString*str2=[strArray1 objectAtIndex:1];
NSArray*strArray2 =[str2 componentsSeparatedByString:#")"];
NSString*strToReplace=[strArray2 objectAtIndex:0];
return [oldStr stringByReplacingOccurrencesOfString:strToReplace withString:#""];
}
This function is valid for the string which contains one pair of parentheses**()**
You can change it as per your requirement.
Hope this helps!
I have a strange problem encoding my String
For example:
NSString *str = #"\u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13";
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %#", utf);
This worked perfectly in log
utf: ฉันรักคุณ
But, when I try using my string that I parsed from JSON with the same string:
//str is string parse from JSON
NSString *str = [spaces stringByReplacingOccurrencesOfString:#"U" withString:#"u"];
NSLog("str: %#, str);
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog("utf: %#", utf);
This didn't work in log
str: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
utf: \u0e09\u0e31\u0e19\u0e23\u0e31\u0e01\u0e04\u0e38\u0e13
I have been finding the answer for hours but still have no clue
Any would be very much appreciated! Thanks!
The string returned by JSON is actually different - it contains escaped backslashes (for each "\" you see when printing out the JSON string, what it actually contains is #"\").
In contrast, your manually created string already consists of "ฉันรักคุณ" from the beginning. You do not insert backslash characters - instead, #"\u0e09" (et. al.) is a single code point.
You could replace this line
NSString *utf = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
with this line
NSString *utf = str;
and your example output would not change. The stringByReplacingPercentEscapesUsingEncoding: refers to a different kind of escaping. See here about percent encoding.
What you need to actually do, is parse the string for string representations of unicode code points. Here is a link to one potential solution: Using Objective C/Cocoa to unescape unicode characters. However, I would advise you to check out the JSON library you are using (if you are using one) - it's likely that they provide some way to handle this for you transparently. E.g. JSONkit does.
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);
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)));