Friday, June 14, 2013

NSCharacterSet newlineCharacterSet example in Objective C (iOS).


NSCharacterSet newlineCharacterSet

Returns a character set containing the newline characters.

+ (id)newlineCharacterSet

Return Value of [NSCharacterSet newlineCharacterSet]
A character set containing the newline characters (U+000A–U+000D, U+0085).

NSCharacterSet newlineCharacterSet example.
Split the string into components and join them by space:

NSString *newString = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];

Example of [NSCharacterSet newlineCharacterSet].
// grab your file
NSMutableArray *data = [[fileString componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] mutableCopy];
for (int i = 0; i < [data count]; i++)
{
    [data replaceObjectAtIndex: i
                    withObject: [[data objectAtIndex: i] componentsSeparatedByString: @","]];
}

NSCharacterSet newlineCharacterSet example.
NSString *address = @"129 City Road \nShoreditch \nLondon EC1V 1JB";   
address = [address stringByReplacingOccurrencesOfString:@"\n" withString:@""];
address = [address stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
This worked and i got the output like :

129 City Road Shoreditch London EC1V 1JB

End of NSCharacterSet newlineCharacterSet example article.