nsstring

How to get substring of NSString?

[亡魂溺海] 提交于 2019-11-27 06:56:40
If I want to get a value from the NSString @"value:hello World:value" , what should I use? The return value I want is @"hello World" . Regexident Option 1: NSString *haystack = @"value:hello World:value"; NSString *haystackPrefix = @"value:"; NSString *haystackSuffix = @":value"; NSRange needleRange = NSMakeRange(haystackPrefix.length, haystack.length - haystackPrefix.length - haystackSuffix.length); NSString *needle = [haystack substringWithRange:needleRange]; NSLog(@"needle: %@", needle); // -> "hello World" Option 2: NSRegularExpression *regex = [NSRegularExpression

Storing and retrieving unsigned long long value to/from NSString

偶尔善良 提交于 2019-11-27 06:55:06
I have an unsigned long long value which I want to store into an NSString and retrieve from the string. Initially I have the value in an NSNumber and I am using this to get the string NSString *numStr = [NSString stringWithFormat:@"%llu", [myNum unsignedLongLongValue]]; where myNum is an NSNumber. To get back the NSNumber from the NSString I have to first get the unsigned long long value. But there is no method in the NSString class to do that (we just have one for getting the long long value, not the unsigned long long value). Can someone please tell me how I can get back the value into an

Objective C: How to extract part of a String (e.g. start with '#')

感情迁移 提交于 2019-11-27 06:53:58
I have a string as shown below, NSString * aString = @"This is the #substring1 and #subString2 I want"; How can I select only the text starting with '#' (and ends with a space), in this case 'subString1' and 'subString2'? Note: Question was edited for clarity You can do this using an NSScanner to split the string up. This code will loop through a string and fill an array with substrings. NSString * aString = @"This is the #substring1 and #subString2 I want"; NSMutableArray *substrings = [NSMutableArray new]; NSScanner *scanner = [NSScanner scannerWithString:aString]; [scanner scanUpToString:@"

Using an NSString in a switch statement

断了今生、忘了曾经 提交于 2019-11-27 06:47:47
Is it possible to use an NSString in a switch statement? Or is it better to just use if / else if ? switch statement requires integer constants for it cases so NSString cannot be used here, so it seems you have to go for if/else option. One more point is you must compare NSStrings using isEqualToString: or compare: method, so even if pointer values have been allowed for switch cases you could not use them anyway user1717750 I use these macros in my app. #define CASE(str) if ([__s__ isEqualToString:(str)]) #define SWITCH(s) for (NSString *__s__ = (s); ; ) #define DEFAULT SWITCH (string) { CASE

NSString to Emoji Unicode

假装没事ソ 提交于 2019-11-27 06:27:46
问题 I am trying to pull an JSON file from the backend containing unicodes for emoji. These are not the legacy unicodes (example: \ue415), but rather unicodes that work cross platform (example: \U0001F604). Here is a sample piece of the json getting pulled: [ { "unicode": "U0001F601", "meaning": "Argh!" }, { "unicode": "U0001F602", "meaning": "Laughing so hard" } ] I am having difficulty converting these strings into unicodes that will display as emoji within the app. Any help is greatly

How to decode / convert a base64 string to NSData?

你说的曾经没有我的故事 提交于 2019-11-27 06:19:08
问题 Hello I am trying to figure out how convert / decode a base64 string in an iOS application to NSData, so I can decrypt the data that I encrypted. The method I used for converting the NSData to a base 64 string can be found here Is there a similar way to create method to decode / convert the base 64 string to NSData? 回答1: This is what I was looking for. + (NSData *)base64DataFromString: (NSString *)string { unsigned long ixtext, lentext; unsigned char ch, inbuf[4], outbuf[3]; short i, ixinbuf;

Escape Quotes in Objective-C

我怕爱的太早我们不能终老 提交于 2019-11-27 06:11:51
问题 I'm using this code snippet to encode characters to be friendly with a POST request: NSString *unescaped = [textField text]; NSString *escapedString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes( NULL, (__bridge_retained CFStringRef)unescaped, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8); Which works great, but does not add escape Quotation marks: " How do I escape Quotation marks in IOS? 回答1: I think I didn't word the question correctly. I

Objective-C – Replace newline sequences with one space

别来无恙 提交于 2019-11-27 06:04:11
问题 How can I replace newline (\n) sequences with one space. I.e the user has entered a double newline ("\n\n") I want that replaced with one space (" "). Or the user has entered triple newlines ("\n\n\n") I want that replaced with also one space (" "). 回答1: Try this: NSArray *split = [orig componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; split = [split filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; NSString *res = [split

NSString allocation and initializing

こ雲淡風輕ζ 提交于 2019-11-27 05:55:47
问题 What is the difference between: NSString *string1 = @"This is string 1."; and NSString *string2 = [[NSString alloc]initWithString:@"This is string 2.]; Why am I not allocating and initializing the first string, yet it still works? I thought I was supposed to allocate NSString since it is an object? In Cocoa Touch, -(IBAction) clicked: (id)sender{ NSString *titleOfButton = [sender titleForState:UIControlStateNormal]; NSString *newLabelText = [[NSString alloc]initWithFormat:@"%@", titleOfButton

NSPredicate Exact Match with String

走远了吗. 提交于 2019-11-27 05:42:26
问题 I have a NSPredicate like this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString]; But that will return anything which contains that string. For example: If my entity.name's where: text texttwo textthree randomtext and the myString was text then all of those strings would match. I would like it so that if myString is text it would only return the first object with the name text and if myString was randomtext it would return the fourth object with