nsstring

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

醉酒当歌 提交于 2019-12-01 02:26:30
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? Easily done by creating a character set of characters you want to keep and using invertedSet to create an "all others" set. Then split the string into an array

How to compare two case insensitive strings?

故事扮演 提交于 2019-12-01 02:18:17
问题 i have 2 string objects containing same string but case is different,now i wanna compare them ignoring the case sensitivity,how to do that??here is the code... #import <Foundation/Foundation.h> void main() { NSString *myString1 = @"mphasis"; NSString *myString2 = @"MPHASIS"; if ([myString1 caseInsenstiveCompare:myString2]) { NSLog (@"ITS EQUAL"); } else { NSLog (@"ITS NOT EQUAL"); } } 回答1: If you look up caseInsensitiveCompare: in the docs you'll see that it returns an NSComparisonResult

Url minus query string in Objective-C

余生长醉 提交于 2019-12-01 02:09:32
What's the best way to get an url minus its query string in Objective-C? An example: Input: http://www.example.com/folder/page.htm?param1=value1&param2=value2 Output: http://www.example.com/folder/page.htm Is there a NSURL method to do this that I'm missing? There's no NSURL method I can see. You might try something like: NSURL *newURL = [[NSURL alloc] initWithScheme:[url scheme] host:[url host] path:[url path]]; Testing looks good: #import <Foundation/Foundation.h> int main(int argc, char *argv[]) { NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init]; NSURL *url = [NSURL URLWithString:@

Is there a difference between NSString compare: and isEqual(ToString):?

南笙酒味 提交于 2019-12-01 02:09:30
问题 Occasionally I find code which tests if two NSString s are the same like this: if ([str1 compare:str2] == NSOrderedSame) { // Do something } Now, I believe this is less readable than using isEqualToString: and it also has some nasty side effects, like if str1 == nil the if(..) evaluates to true, or when str2 == nil havoc might break upon us according to the Apple docs. ( Edit : As hatfinch points out, if str1 == nil && str2 == nil both variants produce the wrong result. So you need to guard

received memory warning. level 1

会有一股神秘感。 提交于 2019-12-01 01:51:14
I am new to iPhone development. I am creating an application in which i need to create buttons and labels programmatically. So I am doing this in .h file @interface FirstQuizViewControlleriPad : UIViewController{ IBOutlet UIButton *startgame; UILabel *theQuestion; IBOutlet UIScrollView *scrollview; UILabel *Question; UILabel *Description; UIView *ContainerView; UILabel *Challengetext; UIImageView *img1; UIImageView *img2; UIImageView *background; IBOutlet UIButton *reviewbtn; UILabel *ans1; UILabel *ans2; UILabel *ans3; UILabel *ans4; UIButton *button; UIButton *button2; UIButton *button3;

How can I convert a NSString representation of a time value into two NSInteger's containing the hour and minute?

社会主义新天地 提交于 2019-12-01 01:14:08
I'm diving into iOS development and the Objective C language and am building an alarm clock app to become familiar with the SDK and language. I have an NSString object that represents a time, with the range "1:00 am" to "12:59 am" . I need to convert this NSString into two NSInteger 's that contain the hour value and minute value. As I'm doing this, I'm finding the NSString manipulation that I'm doing to be extremely laborious and it just feels like sloppy code. Is there a simple way to extract the hour and minute characters from a NSString representation of a time value and store their

How to copy a wchar_t into an NSString?

跟風遠走 提交于 2019-12-01 01:12:49
I'm using stringWithFormat @"%ls" to do it and I only see the first character copied, which makes me think it's still assuming it's a single byte char. Any ideas? Use initWithBytes:length:encoding . You will have to know the encoding that wchar_t uses, I believe it is UTF-32 on Apple platforms. #if defined(__BIG_ENDIAN__) # define WCHAR_ENCODING NSUTF32BigEndianStringEncoding #elif defined(__LITTLE_ENDIAN__) # define WCHAR_ENCODING NSUTF32LittleEndianStringEncoding #endif [[NSString alloc] initWithBytes:mystring length:(mylength * 4) encoding:WCHAR_ENCODING] In general, I suggest avoid using

UILabel detect line breaks

雨燕双飞 提交于 2019-12-01 01:04:30
问题 I am currently using a UILabel to display multiple lines of text. The line break most is set to NSLineBreakByWordWrapping so the the label automatically inserts line breaks. How can I detect where those line breaks are? Basically, I want to return a string with each \n break included. 回答1: Here is my work around for detecting line breaks inside UILabel - (int)getLengthForString:(NSString *)str fromFrame:(CGRect)frame withFont:(UIFont *)font { int length = 1; int lastSpace = 1; NSString

NSString encoding special characters like !@#$%^&

一笑奈何 提交于 2019-12-01 00:23:08
How can i encode my NSString so all the special character for example & becomes &amp and ' becomes &apos? I am not sure if encoding is the right word for it so please correct me if I am wrong. Thanks Regexident What you are talking about is called HTML Entities . There exists a category claiming to solve this: NSString+HTML . For URL Escaping (while we're at it) use this: @nterface NSString (Escaping) - (NSString *)percentEscapedString - (NSString *)percentUnescapedString @end @implementation NSString (Escaping) - (NSString *)percentEscapedString { return [self

How do I get a formatted NSString from format and va_list?

时光毁灭记忆、已成空白 提交于 2019-11-30 23:43:05
问题 I'm developing a static library that will be distributed to other developers, who may need debug statements. So I have several levels of logging. In order to avoid constant appearance of if(loggingLevelCurrentlySet >= loggingLevelWantedForThisInstance){ NSLog(@"log this"); } I created a set of logging function wrappers. A simplified version looks like this: void myLog(int logLevel, NSString *format, va_list args){ if((loggingLevelCurrentlySet >= logLevel)){ NSLogv(format, args); } } void