Tokenize string on multiple characters in Objective-C - 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];

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

Objective-C removing whitespace from strings in array

I want to import a file of strings line by line into an array. I want to get rid of all of the whitespace before and after the strings so that I can compare the strings a lot easier without having them not match due to small whitespace discrepancies. I NSData the content of the files then take the two strings
NSString* string = [[[NSString alloc] initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding] autorelease];
NSString* string2 = [[[NSString alloc] initWithBytes:[data2 bytes]
length:[data2 length]
encoding:NSUTF8StringEncoding] autorelease];
I tried below to remove the whitespace before adding to an array but it does not seem to work.
NSString *newString = [string stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
NSString *newString2 = [string2 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
NSArray *fileInput = [newString componentsSeparatedByString:#"\n"];
NSArray *fileInput2 = [newString2 componentsSeparatedByString:#"\n"];
If you are looking at substituting all occurrences of whitespace then using stringByTrimmingCharactersInSet: won't help as it only trims off at the start and end of the string. You will need to use the stringByReplacingOccurrencesOfString:withString: method to eliminate the whitespace.
NSString * newString = [string stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString * newString2 = [string2 stringByReplacingOccurrencesOfString:#" " withString:#""];
However,
If you want to trim all the strings in the array then you will have to enumerate the array and add the trimmed strings in a new mutable array.
Looks to me like you are removing the white space from the front and back of the whole file but not from each line. Try something like this;
NSArray *fileInput2 = [newString2 componentsSeparatedByString:#"\n"];
NSMutableArray *trimmedFileInput2 = [NSMutableArray array];
for(NSString *gak in fileInput2) {
[trimmedFileInput2 addObject:[gak stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
[Thanks #Deepak for the comment, dooh!]
Both #Deepak and #Bill Dudney being right, I'm just throwing in another way to solve your problem:
NSMutableArray *fileInput = [NSMutableArray array];
[string enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
if ([line length] > 0) {
[fileInput addObject:
[line stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
}
}];
(Disclaimer: Works in iOS 4+, OS X 10.6+ only... but I love blocks! :))