nsstring

Get the NSString height

末鹿安然 提交于 2019-11-27 03:22:25
问题 I have an NSString, and I want to know its height to create an appropriate UILabel. Doing this NSString *string = @"this is an example"; CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:10.0f] forWidth:353.0 lineBreakMode:UILineBreakModeWordWrap]; float height = size.height; height is now 13.0. If I use this string NSString *string = @"this is an example this is an example this is an example this is an example this is an example this is an example this is an example this is an

Replace a character at a certain index in NSString

柔情痞子 提交于 2019-11-27 03:12:23
问题 I'm looking for a way to replace a character at a certain index of a NSString. For example: myString = @"******"; I want to replace the 3rd " * " with an "A", so that the string looks like this: myString = @"**A***"; How can I achieve this? Thanks in advance 回答1: Try with this: NSString *str = @"*******"; str = [str stringByReplacingCharactersInRange:NSMakeRange(3, 1) withString:@"A"]; NSLog(@"%@",str); 回答2: There are really two options; Since NSString is read-only, you need to call

Objective-C: Best way to extract substring from NSString?

喜你入骨 提交于 2019-11-27 03:02:21
问题 I have a space-separated string like @"abc xyz http://www.example.com aaa bbb ccc" . How can I extract the substring @"http://www.example.com" from it? 回答1: I know this is a very late reply, but you can get the substring "http://www.abc.com" with the following code: [@"abc xyz http://www.abc.com aaa bbb ccc" substringWithRange:NSMakeRange(8, 18)] Of course, you can still use an NSString for that. 回答2: Try this: [yourString substringToIndex:<#(NSUInteger)#>]; //or [yourString

How to convert NSNumber to NSString

微笑、不失礼 提交于 2019-11-27 02:59:43
So I have an NSArray "myArray" with NSNumber s and NSString s. I need them in another UIView so i go like this: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil]; details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"]; The subjectText works. But how can I get the NSNumber s out of it? (I actually need them as strings...) I would convert a NSString out of a NSNumber like this: NSString *blah = [NSNumber intValue] . But

How do you detect words that start with “@” or “#” within an NSString?

假如想象 提交于 2019-11-27 02:57:29
问题 I'm building a Twitter iPhone app, and it needs to detect when you enter a hashtag or @-mention within a string in a UITextView. How do I find all words preceded by the "@" or "#" characters within an NSString? Thanks for your help! 回答1: You can use NSRegularExpression class with a pattern like #\w+ (\w stands for word characters). NSError *error = nil; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; NSArray *matches = [regex

What is the purpose of having both NSMutableString and NSString?

佐手、 提交于 2019-11-27 02:57:00
问题 Why does Objective C provide both class NSString and subclass NSMutableString rather than just provide NSMutableString? Isn't a NSString equivalent to "const NSMutableString"? In C++, you have only one string class, std::string, and if you want a constant you declare a const std:string. I'm interested in knowing why I shouldn't just use NSMutableString everywhere and never bother with NSString? There must be a reason, or the language designers wouldn't provide both. maybe it takes up less

Objective-C 101 (retain vs assign) NSString

a 夏天 提交于 2019-11-27 02:51:21
A 101 question Let's say i'm making database of cars and each car object is defined as: #import <UIKit/UIKit.h> @interface Car:NSObject{ NSString *name; } @property(nonatomic, retain) NSString *name; Why is it @property(nonatomic, retain) NSString *name; and not @property(nonatomic, assign) NSString *name; ? I understand that assign will not increment the reference counter as retain will do. But why use retain , since name is a member of the todo object the scope of it is to itself. No other external function will modify it either. Chuck There's no such thing as the "scope of an object" in

Convert NSString separated by comma to NSArray [duplicate]

烂漫一生 提交于 2019-11-27 02:50:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Convert NSArray to NSString in Objective-C I have an array of an NSArray myArray, here is the content: ( "<MPConcreteMediaItem: 0x1b7dd0> 8671085923283003246", "<MPConcreteMediaItem: 0x1b7e50> 16275751483823231324", "<MPConcreteMediaItem: 0x1b7e70> 4976771456698615367" ) I used the code below to assign to an NSString myString: NSString *myString = [myArray description]; And the out put is still as expected: ( "

Remove characters from NSString?

拈花ヽ惹草 提交于 2019-11-27 02:46:55
NSString *myString = @"A B C D E F G"; I want to remove the spaces, so the new string would be "ABCDEFG". Tom Jefferys You could use: NSString *stringWithoutSpaces = [myString stringByReplacingOccurrencesOfString:@" " withString:@""]; If you want to support more than one space at a time, or support any whitespace, you can do this: NSString* noSpaces = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString:@""]; Taken from NSString stringByReplacingOccurrencesOfString:withString: Returns a new string in which all occurrences of a target

how can I convert string to an array with separator?

末鹿安然 提交于 2019-11-27 02:01:29
问题 I have a string in the following format myString = "cat+dog+cow" I need to store each string separated by + in to a array. Eg: myArray[0] = cat myArray[1] = dog myArray[2] = cow Can anyone tell me the proper way to do this? 回答1: componentsSeparatedByString: splits the string and return the result in an array. NSArray *myArray = [myString componentsSeparatedByString:@"+"]; [myArray objectAtIndex:0];//cat [myArray objectAtIndex:1];//dog [myArray objectAtIndex:2];//cow 回答2: Try this.. NSArray