nsstring

Detect whole word in NSStrings

妖精的绣舞 提交于 2019-12-01 21:35:11
问题 How do I detect if an NSString contains a specific word, e.g. is . If the NSString is Here is my string. His isn't a mississippi isthmus. It is... ? The method should detect the word is and return YES . However, if the NSString is His isn't a mississipi isthmus , it should return NO . I tried using if ([text rangeOfString:@"is" options:NSCaseInsensitiveSearch].location != NSNotFound) { ... } but it detects characters not words. 回答1: Use "regular expression" search with the "word boundary

NSDiacriticInsensitivePredicateOption not supported by Realm

為{幸葍}努か 提交于 2019-12-01 21:29:06
I have DictObject with a text property that contains some Vietnamese letters, such as "Sống". @interface DictObj : RLMObject @property NSString *text;//Ex: sống @end I would like to use BEGINSWITH[cd] to perform a diacritic insensitive search, but Realm does not currently support it: RLMResults *fetchedResults = [DictObj objectsInRealm:realm where:@"text BEGINSWITH[cd] %@",inputText]; I can use kCFStringTransformStripDiacritics to transform my input text, but I don't know how to perform that string transformation when the string is already stored in Realm file. Realm Objective-C v2.5.0 added

How could I substring an NSString to the third occurrence of a character?

半腔热情 提交于 2019-12-01 21:28:24
I want to count the ',' characters and substring when it has been repeated 3 times. Input: 9,1,2,3,9 Output: 9,1,2 I am pretty sure something that specific would not have a build in method. Off the top of my head: for(unsigned int i = 0; i < [string length]; ++i) { if([string characterAtIndex:i] == ',') { ++commaCount; if(commaCount == 3) { return [string substringWithRange: NSMakeRange(0, i)]; } } } http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html The apple reference documents are usually a good place to start.

NSDateFormatter doesn't convert my NSString in NSDate

落花浮王杯 提交于 2019-12-01 21:13:41
I have a string and need convert it to a date, but it doesn't convert correctly. I don't know why... my code is: NSString * fecha = @"2011-12-07 11:11:29.657"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.ZZZ"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:fecha]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit

NSString retain count -1

若如初见. 提交于 2019-12-01 21:06:40
I want to print retain count of NSString in AppDelegate class in didFinishLaunchingWithOptions method NSString *str = [[NSString alloc] init]; NSLog(@"str retain count %d",[str retainCount]); but it always gives -1 instead of +1....why ??? NSUIntegerMax means the returned object is immortal. This used to be in the docs, but has since been removed (because -retainCount is very discouraged). In MRC, some people would actually override -retainCount to prevent their singletons from ever being deallocated. In this case, it is a logical optimization for [[NSString alloc] init] to return a constant

Why is NSString stringWithString returning pointer to copied string?

匆匆过客 提交于 2019-12-01 20:55:47
问题 I'm trying to copy an NSString value out of an NSMutableArray into a new variable. NSString stringWithString is returning an NSString with the same memory address as the object in my array. Why? #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSMutableArray *arr = [NSMutableArray arrayWithObject:@"first"]; NSLog(@"string is '%@' %p", [arr objectAtIndex:0], [arr objectAtIndex:0]); // copy the string NSString *copy = [NSString stringWithString:[arr

NSString Backslash escaping

孤者浪人 提交于 2019-12-01 20:45:41
I am working on an iPhone OS application that sends an xml request to a webservice. In order to send the request, the xml is added to an NSString. When doing this I have experienced some trouble with quotation marks " and backslashes \ in the xml file, which have required escaping. Is there a complete list of characters that need to be escaped? Also, is there an accepted way of doing this escaping (ie replacing \ with \\ and " with \" ) or is it a case of creating a method myself? Thanks NSString *escapedString = [unescapedString stringByReplacingOccurrencesOfString:@"\\" withString:@"\\\\"];

Convert TXT File of Unknown Encoding to String

梦想的初衷 提交于 2019-12-01 20:39:00
How can I convert Plain Text (.txt) files to a string if the encoding type is unknown? I'm working on a feature that would allow users to import txt files into my app. This means the file could have been created in any number of apps, utilizing any of a variety of encodings that would be considered valid for a plain text file. My understanding is this could include (ASCII, UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32, UTF-32BE, UTF-32LE, or EBCDIC?!) Things had been going well using the following: NSString *txtFileAsString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding

How to use NULL character with NSString?

妖精的绣舞 提交于 2019-12-01 20:14:01
问题 In PHP, I can call base64_encode("\x00". $username. "\x00". $password) and the "\x00" represents a NULL character. Now, in Objective-C, I have a function that converts NSData to base64 encoded NSString created by DaveDribin. How do I create data from a string that has NULL characters? This doesn't seem to work... NSData * authCode = [[NSString stringWithFormat:@"%c%@%c%@", '\0', self.username, '\0', self.password] dataUsingEncoding:NSUTF8StringEncoding]; 回答1: Like this: char bytes[] = "

Boxing of NSString stringWithFormat like NSNumber

江枫思渺然 提交于 2019-12-01 20:09:39
We can create an NSNumber like this NSNumber *number = [NSNumber numberWithFloat:4.5]; //or NSNumber *number = @(4.5); //or NSNumber *number = @4.5; I know we can convert to an NSString with the following statement NSString *string = @("stuff"); //equivalent of [NSString stringWithUTF8String] However, can we create an NSString like this? NSString *string = @(@"Name is:%@",name); //equivalent of [NSString stringWithFormat] This is just off the top of my head. I do not think there's any syntactic sugar for this. Though, I believe you could achieve what you are looking for like this: Put this in