nsstring

using NSString + stringWithContentsOfFile:usedEncoding:error:

时光怂恿深爱的人放手 提交于 2019-12-04 00:34:30
I've got problem with use + stringWithContentsOfFile:usedEncoding:error: My problem in usedEncoding:(NSStringEncoding *)enc I don't know how can i set pointer to encoding. If i make it - programm is fail. For example, in similar function we have encoding:(NSStringEncoding)enc - without pointer! I want loading file (file has encoding ISOLatin1) in NSString and use NSString as UTF8String. how can i make it ? thanks. NSStringEncoding encoding; NSError* error; NSString* myString = [NSString stringWithContentsOfFile:myFilePath usedEncoding:&encoding error:&error]; 来源: https://stackoverflow.com

How to join two strings for NSPredicate, ie firstname and lastname

↘锁芯ラ 提交于 2019-12-03 23:36:22
I have a Person Object which has two NSString properties; firstName and lastName . I'm currently using an NSPredicate like so: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName contains[cd] %@) OR (lastName contains[cd] %@)", searchText, searchText]; So, for example, say I'm searching for the name "John Smith" . In my search bar if I type "Joh" , then John Smith will appear as an option. This is good and fine, but if I type in "John Sm" it will go blank. How can I join firstName and lastName in the predicate so if I was searching for "John Sm" then John Smith would still

Remove characters in NSCharacterSet from NSString

浪尽此生 提交于 2019-12-03 23:33:38
问题 I have an NSCharacterSet which contains all the characters I want to remove from my NSString. How can I do that? 回答1: If you're not too worried about efficiency, a simple way would be [[myString componentsSeparatedByCharactersInSet:myCharacterSet] componentsJoinedByString:@""] . Otherwise, you could run through the characters in a loop, appending ones that weren't in the set onto a new string. If you do it that way, remember to use an NSMutableString for your result as you're building it up.

Remove all non-numeric characters from an NSString, keeping spaces

倖福魔咒の 提交于 2019-12-03 23:33:19
问题 I am trying to remove all of the non-numeric characters from an NSString , but I also need to keep the spaces. Here is what I have been using. NSString *strippedBbox = [_bbox stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [_bbox length])]; If I give it a NSString of Test 333 9599 999 It will return 3339599999 but I need to keep the spaces in. How can I do this? 回答1: Easily done by creating a character set of characters you

Objective C How to detect one or multiple spaces in a NSString

两盒软妹~` 提交于 2019-12-03 21:59:35
I have seen one answer for my task, but I couldnt find it now. I want to detect whether a string has empty word and contains at least "" or " " (two spaces" or more multiple spaces, OR not. If not, I would add this to Nsmutablearray. If yes with empty or at least one space, I would like it not to be written to mutablearray. How to solve this? EDIT 12 October 2011: Guys, thank you. Again I am sorry that I was unclear about my wish. I wanted to check if a string is empty or contains whitespaces without any character. I have posted my answer below. Im not sure whether it is the most performant

NSNonLossyASCIIStringEncoding equivalent for Android

痴心易碎 提交于 2019-12-03 21:45:33
问题 I got to port some chat code from iOS to Android. Before sending the chat message to the socket, the iOS code uses the NSNonLossyASCIIStringEncoding class as parameter of the NSString::dataUsingEncoding. How would you do it in Android? Same question about the opposite decoding. Without doing that, for instance, the line breaks disappear in the message received on the other mobile. Code on iOS: NSData *data1 = [myStringTosend dataUsingEncoding:NSNonLossyASCIIStringEncoding]; NSString

Case insensitive compare against bunch of strings

醉酒当歌 提交于 2019-12-03 20:37:10
What would be the best method to compare an NSString to a bunch of other strings case insensitive? If it is one of the strings then the method should return YES, otherwise NO. Here's a little helper function: BOOL isContainedIn(NSArray* bunchOfStrings, NSString* stringToCheck) { for (NSString* string in bunchOfStrings) { if ([string caseInsensitiveCompare:stringToCheck] == NSOrderedSame) return YES; } return NO; } Of course this could be greatly optimized for different use cases. If, for example, you make a lot of checks against a constant bunchOfStrings you could use an NSSet to hold lower

NSPredicate to filter between two dates of NSString type

佐手、 提交于 2019-12-03 20:34:46
I've some data (as NSDictionary in an NSMutableArray ), a sample data is like, Consider each row as NSDictionary and entire table is an NSMutableArray contains events. I want to result between two dates, so I'm using NSPredicate to filter my data. Here's my code snippet for this, NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Date >= %@ AND Date <= %@", startDate,endDate]; NSArray *filter = [arrayEvents filteredArrayUsingPredicate:predicate]; Test filtration : Case 1: NSString *startDate = @"01/07/14"; NSString *endDate = @"31/07/2014"; Output: All events (rows). Case 2: NSString

Dynamically format a float in a NSString

余生长醉 提交于 2019-12-03 20:24:50
Consider this: NSString *whatever=[NSString stringWithFormat:@"My float: %.2f",aFloat]; This will round my aFloat to 2 decimal places when building the string whatever Suppose I want the 2 in this statement to be assignable, such that, based on the value of aFloat I might have it show 2 or 4 decimal places. How can I build this into stringWithFormat ? I want to be able to do this without an if that simply repeats the entire line for different cases, but rather somehow dynamically change just the %.2f portion. You will need to build the format first: NSInteger precision = 2; NSString *format =

Converting NSString to NSData and vice versa

爷,独闯天下 提交于 2019-12-03 19:52:20
问题 I am having an issue while trying to convert NSString to NSData and vice versa. I am trying to store encrypted string to my database. For that I am using AES algorithm. Now what I am doing is I get encrypted NSData and I am converting this to NSString using following: // Not woking NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSUTF8StringEncoding]; // Working NSString *strTemp = [[NSString alloc] initWithData:encData encoding:NSASCIIStringEncoding]; Why NSData is not