'"' is getting escaped with CFURLCreateStringByAddingPercentEscapes but not with custom NSCharacterSet - objective-c

'"' is getting escaped with CFURLCreateStringByAddingPercentEscapes but not with custom NSCharacterSet. But when I add '\"' to NSCharacterSet then it is getting escaped.
Why the two behavior between two different apis? Would it be safe to add '\"' to custom NSCharacterSet?
NSString* escAcctQuery1 = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef) jsonQuery, NULL,
(CFStringRef) #"!*();':#&=+$,/?%#[]{}",
kCFStringEncodingUTF8));
Output: #"%7B%22%24or%22%3A%5B%7B%22uninstalled%22%3Afalse%7D%2C%7B%22uninstalled%22%3A%7B%22%24exists%22%3Afalse%7D%7D%5D%2C%22
NSCharacterSet *customCharacterset = [[NSCharacterSet
characterSetWithCharactersInString:#"!*();':#&=+$,/?%#[]{}"] invertedSet];
NSString *escAcctQuery = [jsonQuery
stringByAddingPercentEncodingWithAllowedCharacters:customCharacterset];
Output: #"%7B"%24or"%3A%5B%7B"uninstalled"%3Afalse%7D%2C%7B"uninstalled"%3A%7B"%24exists"%3Afalse%7D%7D%5D%2C"

Related

How to Trim special Characters in a String in Objective c

I want to trim the occurence of special characters in a string
String has :
prevs: Case Number
____________________
In this i want to remove -------- this dash from this string .I have tried like this :
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:#"-"];
NSString *stringNew = [[previousString2 componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:#""];
Thanks in Advance!
Try This:
NSString *stringWithoutDash = [yourString stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSString *trimedString = [myString stringByReplacingOccurrencesOfString:#"-" withString:#""];
Use regular expression, the pattern [\\s_]{4,} searches for 4 and more whitespace or underscore characters.
NSString *trimmedString = [previousString2 stringByReplacingOccurrencesOfString:#"[\\s_]{4,}" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, previousString2.length)];
If the underscore characters are really dashes reaplace the character in the pattern.
You can use below code to remove your special string.
NSString *yourString = #"This is your string with speical character --------";
NSCharacterSet *removedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"--------"];
NSString *finalString = [[yourString componentsSeparatedByCharactersInSet:removedCharacterSet] componentsJoinedByString:#""];
NSLog (#"Your final string : %#", finalString);

Remove unallowed characters from NSString

I want to allow only specific characters for a string.
Here's what I've tried.
NSString *mdn = #"010-222-1111";
NSCharacterSet *allowedCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"+0123456789"];
NSString *trimmed = [mdn stringByTrimmingCharactersInSet:[allowedCharacterSet invertedSet]];
NSString *replaced = [mdn stringByReplacingOccurrencesOfString:#"-" withString:#""];
The result of above code is as below. The result of replaced is what I want.
trimmed : 010-222-1111
replaced : 0102221111
What am I missing here? Why doesn't invertedSet work?
One more weird thing here. If I removed the invertedSet part like
NSString *trimmed = [mdn stringByTrimmingCharactersInSet:allowedCharacterSet];
The result is
trimmed : -222-
I have no idea what makes this result.
stringByTrimmingCharactersInSet: only removes occurrences in the beginning and the end of the string. That´s why when you take inverted set, no feasible characters are found in the beginning and end of the string and thus aren't removed either.
A solution would be:
- (NSString *)stringByRemovingCharacters:(NSString*)str inSet:(NSCharacterSet *)characterSet {
return [[str componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:#""];
}
stringByTrimmingCharactersInSet is only removing chars at the end and the beginning of the string, which results in -222-. You have to do a little trick to remove all chars in the set
NSString *newString = [[origString componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:#""];

truncation of strings, specifically a comma at the end.

I need to truncate a comma at the end of a string, sort of like this:
NSString *string = #" this text has spaces before and after ";
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
instead of whitespaceCharacterSet is there something like commaCharacterSet ?
If every string has a comma/white space at the beginning and end:
NSRange range = NSMakeRange(1, [string length-1]);
NSString *trimmedString = [string substringWithRange:range];
if you just want to trim the comma:
[NSCharacterSet characterSetWithCharactersInString:#","];

Getting digits from an NSString

I have a string like #"(256) 435-8115" or #"256-435-81-15". I need only the digits (this is a phone number). Is this possible? I haven't found an NSString method to do that.
I think there is much simpler way:
-(NSString*)getNumbersFromString:(NSString*)String{
NSArray* Array = [String componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
NSString* returnString = [Array componentsJoinedByString:#""];
return (returnString);
}
Input:
NSString *stringWithPhoneNumber=#"(256) 435-8115";
NSArray *plainNumbersArray=
[stringWithPhoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet decimalDigitCharacterSet]invertedSet]];
NSString *plainNumbers = [plainNumbersArray componentsJoinedByString:#""];
NSLog(#"plain number is : %#",plainNumbers);
OutPut:
plain number is : 2564358115
You can use stringByReplacingOccurrencesOfString: withString: to remove characters you don't want such as #"(" with #""

Tokenize string on multiple characters in Objective-C

I am trying to build a string tokenizer that can tokenize on mutilple characters.
I know I can use:
[string componentsSeparatedByString:#"-"];
but I want to check for white space, dashes, and newlines.
How can this be done?
use:
[string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString: #"\n\t "]]
As Ahmed suggested, use NSCharacterSet to define the delimiter characters, as shown below:
NSString *s = #"foo\nbar baz-quux";
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet whitespaceAndNewlineCharacterSet];
[characterSet addCharactersInString:#"-"];
NSArray *strings = [s componentsSeparatedByCharactersInSet:characterSet];