How to Trim special Characters in a String in Objective c - 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);

Related

How to remove white space between parentheses without affecting the string

I have a string that looks like this:
(
TEST STRING
)
I want to remove the parentheses and the white spaces between the parentheses, but not remove the white space within the string.
Basically, I just want the string. I looked into trimming and replacing, but I don't think they applied here.
Any hint on how to go on about solving this problem?
Thanks
This should work:
NSString *originalString = #"( TEST STRING )";
NSString *newString = [originalString stringByReplacingOccurrencesOfString:#"( " withString:#""];
newString = [newString stringByReplacingOccurrencesOfString:#" )" withString:#""];
I would consider using (NSRegularExpressionSearch | NSAnchoredSearch)
something like this (I'm writing code on the fly so its probably not correct.
NSRange rng = [myString rangeOfString:#"([ \t\n\r]*" options:(NSRegularExpressionSearch | NSAnchoredSearch)];
NSRange rng = [myString rangeOfString:#"[ \t\n\r]*)" options:(NSRegularExpressionSearch | NSAnchoredSearch | NSBackWardsSearch)];
if ( rng.length && rng2.length )
{
myString = [myString substringToIndex:rng2.location];
myString = [myString substringFromIndex:rng.location];
}
Remove the ()
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"()"]];
And trim the white space.
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSCharacterSet *unwantedChar = [NSCharacterSet characterSetWithCharactersInString:#" "#"\"-[}()\n "];
NSString *requiredString = [[string componentsSeparatedByCharactersInSet:unwantedChar] componentsJoinedByString: #""];

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:#""];

How to trim an specific string in IOS?

I have a string that I like to trim the first and last character. What is the equivalent of ltrim/rtrim in IOS?
This is the string:
"["email#email.com","email#email.com"]"
I want to remove only the first and last double quotes.
substringWithRange:
string = [string substringWithRange:(NSRange){1, str.length - 2}];
NSString *str = #" sample string ";
NSString *trimmedString = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

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:#","];

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];