nsstring

Replacing one character in a string in Objective-C

穿精又带淫゛_ 提交于 2019-11-27 04:27:34
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? theChrisKent 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:range: If is just a specific location you can use: stringByReplacingCharactersInRange

Convert first number in an NSString into an Integer?

送分小仙女□ 提交于 2019-11-27 04:27:14
问题 I have an NSString like so: @"200hello" or @"0 something" What I would like to be able to do is take the first occuring number in the NSString and convert it into an int. So that @"200hello" would become int = 200. and @"0 something" would become int = 0. 回答1: int value; BOOL success = [[NSScanner scannerWithString:@"1000safkaj"] scanInteger:&value]; If the number is not always at the beginning: NSCharacterSet* nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; int value = [

Cocoa - Trim all leading whitespace from NSString

吃可爱长大的小学妹 提交于 2019-11-27 04:13:41
问题 (have searched, but not been able to find a simple solution to this one either here, or in Cocoa docs) Q. How can I trim all leading whitespace only from an NSString? (i.e. leaving any other whitespace intact.) Unfortunately, for my purposes, NSString's stringByTrimmingCharactersInSet method works on both leading and trailing. Mac OS X 10.4 compatibility needed, manual GC. 回答1: This creates an NSString category to do what you need. With this, you can call NSString *newString = [mystring

Strip Non-Alphanumeric Characters from an NSString

 ̄綄美尐妖づ 提交于 2019-11-27 04:02:31
问题 I'm looking for a quick and easy way to strip non-alphanumeric characters from an NSString . Probably something using an NSCharacterSet , but I'm tired and nothing seems to return a string containing only the alphanumeric characters in a string. 回答1: We can do this by splitting and then joining. Requires OS X 10.5+ for the componentsSeparatedByCharactersInSet: NSCharacterSet *charactersToRemove = [[NSCharacterSet alphanumericCharacterSet] invertedSet]; NSString *strippedReplacement = [

How to get the width of an NSString?

你。 提交于 2019-11-27 03:59:07
问题 I am trying to get the width of an NSString (ex. NSString *myString = @"hello"). Is there a way to do this? Thanks. 回答1: Here's a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: - (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes]

How to get all NSRange of a particular character in a NSString?

℡╲_俬逩灬. 提交于 2019-11-27 03:48:42
问题 I have two NSStrings: orgText and searchLetter . I want to highlight every occurrences of the searchLetter in the orgText with a red color. How can I get the NSRange of all occurrences of the searchLetter ? for eg : suppose: orgText = "abcahaiapaoiuiapplma" searchLetter = "a". I want to hightlight all "a" occurrences in "abcahaiapaoiuiapplma" with red color. Thanks. 回答1: I wrote this method for my project - SUITextView with highlight: - (NSMutableAttributedString*) setColor:(UIColor*)color

string replace phone number iOS swift

China☆狼群 提交于 2019-11-27 03:42:55
问题 I import a phone-number from "Contacts" and save in NSString . this string contains white-space and I try to delete them using the method: numero = numero.stringByReplacingOccurrencesOfString(" ", withString: "") this method doesn't work. func sostituisci( stringa: NSString! ) -> NSString { var numero: NSString = "" NSLog(stringa) numero = ((stringa as String).stringByReplacingOccurrencesOfString(" ", withString: "") as NSString) NSLog(numero) return numero } the output unchanged log 2014-11

Replace all NSNull objects in an NSDictionary

≯℡__Kan透↙ 提交于 2019-11-27 03:28:42
I'm curious, I currently have an NSDictionary where some values are set to an NSNull object thanks to the help of json-framework. The aim is to strip all NSNull values and replace it with an empty string. I'm sure someone has done this somewhere? No doubt it is probably a four liner and is simple, I am just far too burnt out to figure this out on my own. Thanks! Really simple: @interface NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings; @end @implementation NSDictionary (JRAdditions) - (NSDictionary *)dictionaryByReplacingNullsWithStrings { const

NSJSONSerialization serialization of a string containing forward slashes / and HTML is escaped incorrectly

[亡魂溺海] 提交于 2019-11-27 03:23:30
问题 I am trying to convert some simple HTML into a string value in a JSON object and I'm having trouble getting the string encoding to not escape the string in NSJSONSerialization. Example... I have a string which contains some basic HTML text: NSString *str = @"<html><body><p>Samples / Text</p></body></html>"; The desired outcome is JSON with HTML as the value: { "Title":"My Title", "Instructions":"<html><body><p>Samples / Text</p></body></html>" } I'm using the standard technique to convert an

Why can't LLDB evaluate this expression?

岁酱吖の 提交于 2019-11-27 03:22:38
问题 Neither one of these statements can be processed by LLDB... why is it unable to come up with the NSString result and print it out expr -o -- [NSString stringWithFormat:@"%@", @"Wow this doesnt work??"] po [NSString stringWithFormat:@"%@", @"Wow this doesnt work??"] 回答1: It seems that the expression command in lldb can generally not evaluate functions with variable argument lists. It fails even with a simple C function: int foo(char *msg, ...) { return 17; } (lldb) expr foo("bar") (int) $2 =