nsstring

How to turn a NSString into NSDate?

不想你离开。 提交于 2019-12-21 02:54:06
问题 Ive been racking my brains with no luck. Could someone please tell me how i would convert this string: "2011-01-13T17:00:00+11:00" into a NSDate? 回答1: The unicode date format doc is here Also, for your situation, you could try this: // original string NSString *str = [NSString stringWithFormat:@"2011-01-13T17:00:00+11:00"]; // convert to date NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; // ignore +11 and use timezone name instead of seconds from gmt [dateFormat setDateFormat:

Extract 2 strings from an NSString separated by a special character

爷,独闯天下 提交于 2019-12-20 19:08:08
问题 i have an NSString like "Hello - this is me". I want to search for the "-" and put the text before and after the "-" in two separate strings. Anyone knows how to do that the best way? greets Max 回答1: NSArray *subStrings = [myString componentsSeparatedByString:@"-"]; //or rather @" - " NSString *firstString = [subStrings objectAtIndex:0]; NSString *lastString = [subStrings objectAtIndex:1]; //Add some array range checking to it and you're done. 回答2: NSString *myString = @"123-456-789-1234-2345

NSData to NSString converstion problem!

别等时光非礼了梦想. 提交于 2019-12-20 15:49:55
问题 I'm getting an html file as NSData and need to parse it to extract some info. My approach was to convert it to NSString with UTF8 encoding (the html has non english characters, russian for example) - it failed. I used something like that: NSString *respData = [NSString stringWithUTF8String:[theData bytes]]; but it returned nil. The only thing that actually worked was [NSString stringWithCString:[theData bytes] length:[theData length]]; but when it encounters russian characters for example it

NSData to NSString converstion problem!

送分小仙女□ 提交于 2019-12-20 15:47:29
问题 I'm getting an html file as NSData and need to parse it to extract some info. My approach was to convert it to NSString with UTF8 encoding (the html has non english characters, russian for example) - it failed. I used something like that: NSString *respData = [NSString stringWithUTF8String:[theData bytes]]; but it returned nil. The only thing that actually worked was [NSString stringWithCString:[theData bytes] length:[theData length]]; but when it encounters russian characters for example it

Returning an NSString from an NSError

女生的网名这么多〃 提交于 2019-12-20 15:44:37
问题 I am using the NSURLRequest class in my iPhone app, and the method that calls it returns an NSString which is great for when the connection goes through ok, but the issue is I need to convert the NSError into an NSString so I can either return it back or run some if() statements on it. Any ideas? :) 回答1: -[NSError localizedDescription]. (Also, every ObjC object inherited from NSObject implements -description which returns an NSString.) 回答2: for folks new to objective c (me), following is

Incorrect decrement of the reference count of an object that is not owned at this point by the caller

隐身守侯 提交于 2019-12-20 15:25:15
问题 Incorrect decrement of the reference count of an object that is not owned at this point by the caller on iPhone. It is happening with NSString which I clearly init and release within the for loop. I have tried to do the same as an autoreleases string but I get leaks. I assume the culprit is the stringbytrimming call. Any suggestions, by the way this does not leak, but I get the warning in build and analyze. Everything also works fine and the app does not crash. for(int i=0;i<storyQuantity;i++

Split an NSString into an array in Objective-C

醉酒当歌 提交于 2019-12-20 12:34:38
问题 How can I split the string @"Hello" to either: a C array of 'H' , 'e' , 'l' , 'l' , 'o' or: an Objective-C array of @[@"H", @"e", @"l", @"l", @"o"] 回答1: If you're satisfied with a C array of char s, try: const char *array = [@"Hello" UTF8String]; If you need an NSArray, try: NSMutableArray *array = [NSMutableArray array]; NSString *str = @"Hello"; for (int i = 0; i < [str length]; i++) { NSString *ch = [str substringWithRange:NSMakeRange(i, 1)]; [array addObject:ch]; } And array will contain

Replace a substring with another substring

。_饼干妹妹 提交于 2019-12-20 11:09:18
问题 I want to replace the string "abc" to "def" each time it appears in my NSString object: "axcd abc amamam dff abc kdj abc" How do I do that?? Thanks, Sagiftw 回答1: Try stringByReplacingOccurrencesOfString:withString: . NSString* foo = @"axvc abc amamam dff abc kjd abc"; NSString* bar = [foo stringByReplacingOccurrencesOfString:@"abc" withString:@"def"]; NSLog("%@", bar); 回答2: Check out replacing regular expressions 来源: https://stackoverflow.com/questions/3613843/replace-a-substring-with-another

How to get NSRange(s) for a substring in NSString? [duplicate]

浪子不回头ぞ 提交于 2019-12-20 10:57:24
问题 This question already has answers here : How to get all NSRange of a particular character in a NSString? (4 answers) Closed 6 years ago . NSString *str = @" My name is Mike, I live in California and I work in Texas. Weather in California is nice but in Texas is too hot..."; How can I loop through this NSString and get NSRange for each occurrence of "California", I want the NSRange because I would like to change it's color in the NSAttributed string. NSRange range = NSMakeRange(0,

Concatenate number with string in Swift

百般思念 提交于 2019-12-20 10:16:01
问题 I need to concatenate a String and Int as below: let myVariable: Int = 8 return "first " + myVariable But it does not compile, with the error: Binary operator '+' cannot be applied to operands of type 'String' and 'Int' What is the proper way to concatenate a String + Int? 回答1: If you want to put a number inside a string, you can just use String Interpolation: return "first \(myVariable)" 回答2: You have TWO options; return "first " + String(myVariable) or return "first \(myVariable)" 回答3: To