nsstring

NSString get -characters

岁酱吖の 提交于 2019-12-17 16:29:59
问题 I want to get the characters of an NSString. Like this: NSString *data; const char * typein = [[data characters] UTF8String]; But obviously NSString won't respond to -characters. How do I get the characters of NSString? thanks, Elijah 回答1: You can use this function: for(int i =0 ;i<[myString length]; i++) { char character = [myString characterAtIndex:i]; } or NSString *str = @"astring"; const char *cString = [str UTF8String]; 回答2: If you just want to get a cString from the NSString just call

NSData to NSString with JSON response

爷,独闯天下 提交于 2019-12-17 16:22:17
问题 NSData* jsonData is the http response contains JSON data. NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; NSLog(@"jsonString: %@", jsonString); I got the result: { "result": "\u8aaa" } What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"? 回答1: If you convert the JSON data { "result" : "\u8aaa" } to a NSDictionary (e.g. using NSJSONSerialization ) and print the dictionary NSError *error; NSDictionary

How to create a NSString from a format string like @“xxx=%@, yyy=%@” and a NSArray of objects?

谁都会走 提交于 2019-12-17 15:42:13
问题 Is there any way to create a new NSString from a format string like @"xxx=%@, yyy=%@" and a NSArray of objects? In the NSSTring class there are many methods like: - (id)initWithFormat:(NSString *)format arguments:(va_list)argList - (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList + (id)stringWithFormat:(NSString *)format, ... but non of them takes a NSArray as an argument, and I cannot find a way to create a va_list from a NSArray... 回答1: It is actually not

String won't url encode in iOS

浪子不回头ぞ 提交于 2019-12-17 12:30:12
问题 I'm seriously having a brain fart here, but I can't figure out why this isn't encoding for the life of me. Been searching all over, and I can't even get the code samples to encode. Any ideas? NSString *searchString = @"waffl&es"; NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *urlString = [NSString stringWithFormat:@"http://en.wikipedia.org/?search=%@", encodedSearchString]; NSURL *url = [NSURL URLWithString:urlString];

NSString : easy way to remove UTF-8 accents from a string?

僤鯓⒐⒋嵵緔 提交于 2019-12-17 10:29:09
问题 I want to change a sentence, for example: Être ou ne pas être. C'était là-bas. Would become: Etre ou ne pas etre. C'etait la-bas. Is there any easy way to do this with NSString? Or do I have to develop this on my own by checking each char? 回答1: NSString *str = @"Être ou ne pas être. C'était là-bas."; NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"%@", newStr)

Converting NSString to NSDictionary / JSON

折月煮酒 提交于 2019-12-17 10:15:23
问题 I have the following data saved as an NSString : { Key = ID; Value = { Content = 268; Type = Text; }; }, { Key = ContractTemplateId; Value = { Content = 65; Type = Text; }; }, I want to convert this data to an NSDictionary containing the key value pairs. I am trying first to convert the NSString to a JSON objects as follows : NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; However when I try :

+[NSString stringWithString:] — what's the point?

别等时光非礼了梦想. 提交于 2019-12-17 09:47:37
问题 As NSString strings are immutable, what is the value of the stringWithString: class method? I get the utility when used with NSMutableString , I just didn't see the utility with the NSString class. 回答1: You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate. NSMutableString *buffer = [NSMutableString string]; // do something with buffer NSString *immutableStringToKeepAround = [NSString stringWithString:buffer]; Of course, you can also just make a

Find all locations of substring in NSString (not just first)

拟墨画扇 提交于 2019-12-17 09:22:11
问题 There is a substring that occurs in a string several times. I use rangeOfString , but it seems that it can only find the first location. How can I find all the locations of the substring? NSString *subString1 = @"</content>"; NSString *subString2 = @"--\n"; NSRange range1 = [newresults rangeOfString:subString1]; NSRange range2 = [newresults rangeOfString:subString2]; int location1 = range1.location; int location2 = range2.location; NSLog(@"%i",location1); NSLog(@"%i",location2); 回答1: You can

Replacing one character in a string in Objective-C

青春壹個敷衍的年華 提交于 2019-12-17 07:26:43
问题 Hoping somebody can help me out - I would like to replace a certain character in a string and am wondering what is the best way to do this? I know the location of the character, so for example, if I want to change the 3rd character in a string from A to B - how would I code that? 回答1: If it is always the same character you can use: stringByReplacingOccurrencesOfString:withString: If it is the same string in the same location you can use: stringByReplacingOccurrencesOfString:withString:options

Convert NSString to NSInteger?

随声附和 提交于 2019-12-17 07:14:05
问题 I want to convert string data to NSInteger . 回答1: If the string is a human readable representation of a number, you can do this: NSInteger myInt = [myString intValue]; 回答2: [myString intValue] returns a cType "int" [myString integerValue] returns a NSInteger . In most cases I do find these simple functions by looking at apples class references, quickest way to get there is click [option] button and double-click on the class declarations (in this case NSString ). 回答3: I've found this to be the