nsstring
Convert NSMutableAttributedString to NSString
You can’t use a cast to convert an object from one type to another. Use the provided method: label1.text = [string1 string]; Better yet, use the attributed string: label1.attributedText = string1
Simple string concatenation in Objective C
This code here is working for me NSString *s = @”avant”; s = [s stringByAppendingString:@” – après”]; NSLog(@”%@”, s); 2012-01-13 11:48:59.442 tabbar[604:207] avant – après So my guess is that your you is a bad pointer that is not nil and not the NSString you think it have. Have you try an NSLog on that …
Number of Occurrences of a Character in NSString
You can do this in one line. For example, this counts the number of spaces: NSUInteger numberOfOccurrences = [[yourString componentsSeparatedByString:@” “] count] – 1;
How to use drawInRect:withAttributes: instead of drawAtPoint:forWidth:withFont:fontSize:lineBreakMode:baselineAdjustment: in iOS 7
You can use NSDictionary and apply attributes like this: NSFont *font = [NSFont fontWithName:@”Palatino-Roman” size:14.0]; NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, [NSNumber numberWithFloat:1.0], NSBaselineOffsetAttributeName, nil]; Use attrsDictionary as argument. Refer: Attributed String Programming Guide Refer: Standard Attributes SWIFT: IN String drawInRect is not available but we can use NSString instead: let font = UIFont(name: …
Objective-C: Find numbers in string
Here’s an NSScanner based solution: // Input NSString *originalString = @”This is my string. #1234″; // Intermediate NSString *numberString; NSScanner *scanner = [NSScanner scannerWithString:originalString]; NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@”0123456789″]; // Throw away characters before the first number. [scanner scanUpToCharactersFromSet:numbers intoString:NULL]; // Collect numbers. [scanner scanCharactersFromSet:numbers intoString:&numberString]; // Result. int number = [numberString integerValue]; (Some of …
Search through NSString using Regular Expression
You need to use NSRegularExpression class. Example inspired in the documentation: NSString *yourString = @””; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”(NS|UI)+(\\w+)” options:NSRegularExpressionCaseInsensitive error:&error]; [regex enumerateMatchesInString:yourString options:0 range:NSMakeRange(0, [yourString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ // your code to handle matches here }];
How to find beginswith word in a string using NSPredicate?
Simply use BEGINSWITH instead of CONTAINS. Edit If you need to search in every word of a string, there is a technique which was presented in one of the talks in WWDC 2010. The basic idea is to create a separate entity Word which contains a single word and a reference of the containing object …
NSData to display as a string
Use the NSString initWithData:encoding: method. NSString *someString = [[NSString alloc] initWithData:hashedData encoding:NSASCIIStringEncoding]; (edit to respond to your comment:) In that case, Joshua’s answer does help: NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@”< >”]; NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];
How to make an NSString path (file name) safe
This will remove all invalid characters anywhere in the filename based on Ismail’s invalid character set (I have not verified how complete his set is). – (NSString *)_sanitizeFileNameString:(NSString *)fileName { NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@”/\\?%*|\”<>”]; return [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@””]; } Credit goes to Peter N Lewis for the idea to use componentsSeparatedByCharactersInSet: NSString – Convert …