Objective-C/API
ʸ»úÎó(NSString)
À¸À®¡¦½é´ü²½
- NSString *str1 = [[NSString alloc] init];
- À¸À®¡¦¶õʸ»úÎó¤Ç½é´ü²½¡£»²¾È¥«¥¦¥ó¥¿¤È¤ÏÊÌ´ÉÍý¤Î¤¿¤á²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£
- NSString *str1 = @"";
- Ʊ¾å
- NSString *str1 = [[NSString alloc] initWithString:@"abc"];
- À¸À®¡¦»ØÄêʸ»úÎó¤Ç½é´ü²½¡£»²¾È¥«¥¦¥ó¥¿¤È¤ÏÊÌ´ÉÍý¤Î¤¿¤á²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£
- NSString *str1 = @"abc";
- Ʊ¾å
- NSString *str1 = [NSString stringWithString:@"abc"];
- ¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¤ÇÀ¸À®¡¦½é´ü²½¡£»²¾È¥«¥¦¥ó¥¿¤È¤ÏÊÌ´ÉÍý¤Î¤¿¤á²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£
- NSString *str1 = [NSString stringWithFormat:@"abc"];
- ¥Õ¥©¡¼¥Þ¥Ã¥ÈÉÕ¤¤Î¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¤ÇÀ¸À®¡¦½é´ü²½Î㣱¡£¥ª¡¼¥È¥ê¥ê¡¼¥¹¥×¡¼¥ë¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£¤¢¤¨¤Ærelease¤Ç²òÊü¤·¤Æ¤âOK¡£
- NSString *str1 = [NSString stringWithFormat:@"i: %d", i];
- ¥Õ¥©¡¼¥Þ¥Ã¥ÈÉÕ¤¤Î¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¤ÇÀ¸À®¡¦½é´ü²½Î㣲¡£¥ª¡¼¥È¥ê¥ê¡¼¥¹¥×¡¼¥ë¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£¤¢¤¨¤Ærelease¤Ç²òÊü¤·¤Æ¤âOK¡£
¢¨°Ê¹ß¤ÎÀâÌÀ¤Ç¤Ï²òÊü¤Ë¤Ä¤¤¤Æ¾Êά¤·¤Æ¤¤¤Þ¤¹¡£
´ðËÜ
¢¨°Ê¹ß¤ÎÀâÌÀ¤Ç¤Ï¡ÖNSString *str1 = ¡×¤ÎÉôʬ¤ò¾Êά¤·¤Æ¤¤¤Þ¤¹¡£NSStringʸ»úÎó¤ÏÉÔÊѤǡ¢Ê¸»úÎó¤Î¼è¤ê½Ð¤·¤Ê¤É¤â¿·¤¿¤Ê¥¤¥ó¥¹¥¿¥ó¥¹¤¬ÊÌÅÓÀ¸À®¤µ¤ì¡¢Ê̤λ²¾ÈÊÑ¿ô¤Ç¼õ¤±¤¿¤ê¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£Ê¸»úÎó°Ê³°¤òÊÖ¤¹¥á¥½¥Ã¥É¤Ë¤Ä¤¤¤Æ¤ÏÀâÌÀ¤Ë²¿¤òÊÖ¤¹¤«µ½Ò¤·¤Æ¤¤¤Þ¤¹¡£
- [str1 stringByAppendingString:str2]
- ʸ»úÎó¤ÎÏ¢·ë¤ò¹Ô¤¤¤Þ¤¹¡£
- [str1 stringByAppendingFormat:@"i: %d", i]
- ¥Õ¥©¡¼¥Þ¥Ã¥È¤ÇÀ°·Á¤·¤¿Ê¸»úÎó¤ÎÏ¢·ë¤ò¹Ô¤¤¤Þ¤¹¡£
- [str1 length]
- ʸ»úÎó¤Î¥Ð¥¤¥È¿ô¤òÊÖ¤·¤Þ¤¹¡£
- [str1 description]
- self¤òÊÖ¤·¤Þ¤¹¡£
- [str1 isEqualToString:str2]
- ʸ»úÎ󤬰ìÃפ¹¤ë¤«Èæ³Ó¤·¤Þ¤¹¡£°ìÃפ¹¤ë¾ì¹ç¤ÏYES¤òÊÖ¤·¤Þ¤¹¡£
- [str1 compare:str2]
- ¼½ñ½ç¤ËÈæ³Ó¤·¤Þ¤¹¡£·ë²Ì¤ÏNSOrderedAscending, NSOrderedSame, NSOrderedDescending¤Î¤É¤ì¤«¤Ç¤¹¡£
- [str1 caseInsensitiveCompare:str2]
- Âçʸ»ú¾®Ê¸»ú¤ò¶èÊ̤»¤º¤Ë¼½ñ½ç¤ËÈæ³Ó¤·¤Þ¤¹¡£·ë²Ì¤ÏNSOrderedAscending, NSOrderedSame, NSOrderedDescending¤Î¤É¤ì¤«¤Ç¤¹¡£
¢¨Èæ³Ó¤ÇÍøÍѲÄǽ¤Êoptions:¤Ë¤Ä¤¤¤Æ¤Ï¸åÆüÀ°ÍýͽÄê
°ìÉô¼è¤ê½Ð¤·
- [str1 substringWithRange:NSMakeRange(5, 15)]
- »ØÄꤷ¤¿Ê¸»ú°ÌÃÖ¤ÈŤµ¤Îʸ»úÎó¤ò¼è¤ê½Ð¤·¤Þ¤¹¡£
- [str1 substringToIndex:5]
- ÀèƬ¤«¤é»ØÄꤷ¤¿Ê¸»ú°ÌÃ֤ޤǤÎʸ»úÎó¤ò¼è¤ê½Ð¤·¤Þ¤¹¡£Ê¸»ú°ÌÃÖ¤Îʸ»ú¤Ï´Þ¤ß¤Þ¤»¤ó¡£
- [str1 substringFromIndex:5]
- »ØÄꤷ¤¿Ê¸»ú°ÌÃÖ¤«¤éËöÈø¤Þ¤Ç¤Îʸ»úÎó¤ò¼è¤ê½Ð¤·¤Þ¤¹¡£Ê¸»ú°ÌÃÖ¤Îʸ»ú¤ò´Þ¤ß¤Þ¤¹¡£
- lineRangeForRange¤ÈsubstringWithRange¤òÍѤ¤¤¿£±¹Ô¼è¤ê½Ð¤·
- ²þ¹Ôʸ»ú¤ò¶èÀÚ¤ê¤È¤·¤Æ£±¹Ô¤º¤Ä¼è¤ê½Ð¤·¤Þ¤¹¡£
int total_length = [str1 length]; NSRange range = NSMakeRange(0, total_length); while (range.location < total_length) { range = [str1 lineRangeForRange:NSMakeRange(range.location, 0)]; // ²òÀϳ«»Ï°ÌÃÖ¤«¤é£±¹Ô¤ÎÈϰϤò¼èÆÀ NSLog([string substringWithRange:range]); // ʸ»úÎó¤ò¼è¤ê½Ð¤·ÍøÍѤ·¤Þ¤¹¡£¤³¤ÎÉôʬ¤òŬµ¹½ñ¤Âؤ¨¤Þ¤¹¡£ range.location = NSMaxRange(range); // ²òÀϳ«»Ï°ÌÃÖ¤ò¹¹¿· }
È÷¹Í¡§lineRangeForRange:°ú¿ô¤Îlength¤ÏÄ̾ï0¤ò»ØÄꤷ¤Þ¤¹¡£Ê¸»ú¿ô¤ò»ØÄꤹ¤ë¤È¡¢¤½¤Îʬ¤ò´Þ¤ó¤À°Ê¹ß¤Î²þ¹Ô¤Ç¶èÀÚ¤ê¤Þ¤¹¡£
²þ¹Ôʸ»ú°ìÍ÷
ɽµ | ÀâÌÀ |
\r, 0x0d | CR, ½¾Íè¤ÎMac OS¤Î²þ¹Ôʸ»ú |
\n, 0x0a | LF, Unix¤Î²þ¹Ôʸ»ú |
\r\n | CRLF, Windows¤Î²þ¹Ôʸ»ú |
U+2028 | Unicode¤Î¶èÀÚ¤êʸ»ú |
U+2029 | Unicode¤Î¥Ñ¥é¥°¥é¥Õ¶èÀÚ¤êʸ»ú |
¸¡º÷
- [str1 rangeOfString:str2]
- ºÇ½é¤Ë¸«¤Ä¤«¤Ã¤¿°ÌÃÖ¤òNSRange¹½Â¤ÂΤÇÊÖ¤·¤Þ¤¹¡£¸«¤Ä¤«¤é¤Ê¤¤¤È¤¤Ï¹½Â¤ÂΤÎlocation¤ËNSNotFound¤òÊÖ¤·¤Þ¤¹¡£
ÃÖ´¹
- [str1 stringByReplacingCharactersInRange:range withString:str2]
- »ØÄꤷ¤¿ÈϰϤÎʸ»úÎó¤òÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
- [str1 stringByReplacingOccurrencesOfString:str_to_find withString:str2]
- »ØÄꤷ¤¿Ê¸»úÎó¤ò¤¹¤Ù¤ÆÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
À°·Á
¶õÇòºï½ü¤Ê¤É¸åÆüÀ°ÍýͽÄê
·¿ÊÑ´¹¡Êʸ»ú¢ª¿ôÃÍÅù¡Ë
¼° | ·ë²Ì | ÊÑ´¹ÆâÍÆ |
[@"123" intValue] | 123 | 10¿Ê¿ôʸ»úÎó¢ªintÃÍ |
[@"123" longLongValue] | 123 | 10¿Ê¿ôʸ»úÎó¢ªlong longÃÍ |
[@"1.23" floatValue] | 1.23 | floatÃÍʸ»úÎó¢ªfloatÃÍ |
[@"1.23" doubleValue] | 1.23 | doubleÃÍʸ»úÎó¢ªdoubleÃÍ |
[@"YES" boolValue] | YES | BOOLÃÍʸ»úÎó¢ªBOOLÃÍ È÷¹Í¡§¿ôÃÍʸ»úÎó¤È¸«¤ÆÃͤ¬£°¤«¤É¤¦¤«¡¢¤½¤ì°Ê³°¤ÏÀèƬʸ»ú¤¬Y, y, N, n¤ÇȽÃÇ¡£@"001", @"Yeh!"¤Ï¶¦¤ËYES¤È¤Ê¤ë |
[@"123" integerValue] | NSInteger·¿¤Î123 | 10¿Ê¿ôʸ»úÎó¢ªNSIntegerÃÍ |
ʸ»ú¥³¡¼¥ÉÊÑ´¹
»ØÄê²Äǽ¤Êʸ»ú¥³¡¼¥É°ìÍ÷
NSASCIIStringEncoding | 7bit ASCII¥³¡¼¥É |
NSUTF8StringEncoding | Unicode(UTF-8) |
NSShiftJISStringEncoding | ÆüËܸìShift JISʸ»ú¥³¡¼¥É |
NSISO2022JPStringEncoding | ÅŻҥ᡼¥ë¤Ê¤É¤Ç»È¤ï¤ì¤ëJIS¥³¡¼¥É |
NSJapaneseEUCStringEncoding | ÆüËܸìUNIX¥·¥¹¥Æ¥à¤Îɸ½àʸ»ú¥³¡¼¥É |
NSMacOSRomanStringEncoding | Mac OS¤Î¥¨¥ó¥³¡¼¥É |
- NSString *str1 = [[NSString alloc] initWithCString:"abc" encoding:NSUTF8StringEncoding];
- Cʸ»úÎó¤ÇÀ¸À®¡¦½é´ü²½¤·¤Þ¤¹¡£
- NSString *str1 = [NSString stringWithCString:"abc" encoding:NSUTF8StringEncoding];
- Ʊ¾å¡Ê¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¡Ë
- char *cstr1 = [str1 cStringUsingEncoding:NSUTF8StringEncoding];
- Cʸ»úÎó¤òÀ¸À®¡¦½é´ü²½¤·¤Þ¤¹¡£¡Ê¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¡Ë
- int byte_count = [str1 lengthOfBytesUsingEncoding:NSShiftJISStringEncoding]
- »ØÄꤷ¤¿¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤È¤·¤¿¾ì¹ç¤ËɬÍפʥХ¤¥È¿ô¤òÊÖ¤·¤Þ¤¹¡£ËöÈø¤Î¥Ì¥ëʸ»ú¤Ï´Þ¤ß¤Þ¤»¤ó¡£
initWithData:encoding:, dataUsingencoding, canBeConvertedToEncoding¤Ë¤Ä¤¤¤Æ¤Ï¸åÆüÀ°ÍýͽÄê¡£
web·Ï¤ÎÊÑ´¹
URL¥¨¥ó¥³¡¼¥É¤·¤¿Ê¸»úÎó¤òÊÖ¤¹¤È¤¤Ï¥µ¥ó¥×¥ë¤Î¤è¤¦¤Ëµ½Ò¤·¤Þ¤¹¡£
stringByAddingPercentEscapesUsingEncoding:¥á¥½¥Ã¥É¤Ï¡Ö(0¡ß20)"#%><[\]^`{|}¡×¤·¤«¥¨¥¹¥±¡¼¥×¤»¤º¡¢¡Ö$&+,/:;=?@¡×¤ò¥¨¥¹¥±¡¼¥×¤·¤Ê¤¤¤è¤¦¤Ç¤¹¡£¼¡¤Î¤è¤¦¤Ë¤¹¤ë¤Î¤¬ÄêÀФΤ褦¤Ç¤¹¡£
NSString* encodeURIComponent(NSString* s) { return [((NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)s, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8)) autorelease]; }
¤½¤Î¾ÊÑ´¹
- [str1 lowercaseString]
- ¾®Ê¸»ú¤ËÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
- [str1 uppercaseString]
- Âçʸ»ú¤ËÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
- [str1 capitalizedString]
- ñ¸ì¤ÎÀèƬ¤À¤±Âçʸ»ú¤ËÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
¥Ñ¥¹¤ÎÁàºî
³ÈÄ¥»Ò¤ò¼èÆÀ¤¹¤ëpathExtension¤Ê¤É¿¿ô¤Î¥á¥½¥Ã¥É¤¬Í¤ê¤Þ¤¹¡£¾ÜºÙ¤Ë¤Ä¤¤¤Æ¤Ï¸åÆüÀ°ÍýͽÄê
¥Õ¥¡¥¤¥ëÆþ½ÐÎÏ
¸åÆüÀ°ÍýͽÄê propertyList¥á¥½¥Ã¥É¤òÍѤ¤¤¿¥×¥í¥Ñ¥Æ¥£¥ê¥¹¥È¥Õ¥¡¥¤¥ë¤«¤é¤Î¥Ç¡¼¥¿Æɤ߽Ф·¤Ï¼¡¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤ http://blog.syuhari.jp/archives/1723
¥×¥í¥Ñ¥Æ¥£¥ê¥¹¥È¥Õ¥¡¥¤¥ë¡§XML¤ÈƱÍͤʵ½Ò¤Î¥Õ¥¡¥¤¥ë¡£³ÈÄ¥»Ò¤Ïplist¤ÇXcode¤«¤éÊÔ½¸²Äǽ
NSMutableString
Êѹ¹Áàºî¤¬²Äǽ¤Êʸ»úÎó¤Ç¤¹¡£¥¤¥ó¥¹¥¿¥ó¥¹ÆâÍƤòľÀÜÊѹ¹¤·¤Þ¤¹¡£NSString¤Î¥µ¥Ö¥¯¥é¥¹¤Ç¤¹¤Î¤Ç¡¢NSString¤Î¥á¥½¥Ã¥É¤â»È¤¨¤Þ¤¹¡£
À¸À®¡¦½é´ü²½
- NSMutableString *mstr = [NSMutbleString stringWithCapacity:1000];
- ¥á¥â¥êÍÆÎ̤ò»ØÄꤷ¤ÆÀ¸À®¡¦½é´ü²½¤·¤Þ¤¹¡Ê¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¥³¥ó¥¹¥È¥é¥¯¥¿¡Ë¡£¥á¥â¥ê¤ÏɬÍפʻþÅÀ¤Ç³ÈÄ¥¤µ¤ì¤Þ¤¹¡£¥ª¡¼¥È¥ê¥ê¡¼¥¹¥×¡¼¥ë¤ËÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¤Î¤Ç²òÊü¤Ïµ¤¤Ë¤·¤Ê¤¯¤ÆOK¡£¤¢¤¨¤Ærelease¤Ç²òÊü¤·¤Æ¤âOK¡£¥³¥ó¥Ó¥Ë¥¨¥ó¥¹¤Ç¤Ê¤¤initWithCapacity:¤âͤê¤Þ¤¹¤¬¡¢¥ª¡¼¥È¥ê¥ê¡¼¥¹¥×¡¼¥ë¤ËÅÐÏ¿¤·¤Æ¤¤¤ë¤È²òÊü¤·Ëº¤ì¤Ê¤¤¤Î¤Ç¤³¤Á¤é¤ò»È¤¨¤ÐÎɤµ¤½¤¦¤Ç¤¹¡£¤¿¤À¤·¥¤¥Ù¥ó¥È½èÍý¤¬½ª¤ï¤ë¤È¥ª¡¼¥È¥ê¥ê¡¼¥¹¥×¡¼¥ë¤Ï³«Êü¤µ¤ì¤ë¤Î¤Ç¡¢¥¤¥Ù¥ó¥È½èÍý¤ò¤Þ¤¿¤°¾ì¹ç¤Ïľ¸å¤Ëretain¤·¡¢ÉÔÍפʤȤ³¤í¤Çrelease¤¹¤ëɬÍפ¬¤¢¤ê¤Þ¤¹¡£
ÀßÄê
¥á¥½¥Ã¥É¤¬ÊÖ¤¹¤Î¤Ïvoid¤Ç¤¹¡£¥¤¥ó¥¹¥¿¥ó¥¹ÆâÍƤòľÀÜÊѹ¹¤¹¤ë¤¿¤á¡¢¼°¤Îº¸Â¦¤Ë¡ÖÊÑ¿ô = ¡×¤È¼õ¤±¤Æ¤Ï¤¤¤±¤Þ¤»¤ó¡Ê°Ê¹ß¤ÎÀâÌÀ¤âƱÍÍ¡Ë¡£
- [mstr setString:@"abc"];
- »ØÄꤷ¤¿Ê¸»úÎó¤òÀßÄꤷ¤Þ¤¹¡Ê¥³¥Ô¡¼¡Ë¡£
ÄɲÃ
- [mstr appendString:str2];
- ËöÈø¤ËÄɲä·¤Þ¤¹¡£
- [mstr appendFormat:@"i: %d", i];
- ËöÈø¤Ë¥Õ¥©¡¼¥Þ¥Ã¥ÈÀ°·Á¤·¤¿Ê¸»úÎó¤òÄɲä·¤Þ¤¹¡£
ÁÞÆþ
- [mstr insertString:str2 atIndex:5];
- »ØÄꤷ¤¿Ê¸»ú°ÌÃÖ¤ËÁÞÆþ¤·¤Þ¤¹¡£
ÃÖ´¹
- [mstr replaceCharactersInRange:range withString:str2];
- »ØÄꤷ¤¿ÈϰϤÎʸ»úÎó¤òÃÖ´¹¤·¤¿Ê¸»úÎó¤òÊÖ¤·¤Þ¤¹¡£
- int replace_count = [mstr replaceOccurrencesOfString:str_to_find withString:str2];
- »ØÄꤷ¤¿Ê¸»úÎó¤ò¤¹¤Ù¤ÆÃÖ´¹¤·¤Þ¤¹¡£ÃÖ´¹²ó¿ô¤òÊÖ¤·¤Þ¤¹¡£ ¢¨ÍøÍѲÄǽ¤Êoptions:¤ärange:¤Ë¤Ä¤¤¤Æ¤Ï¸åÆüÀ°ÍýͽÄê
ºï½ü
- [mstr deleteCharactersInRange:NSMakeRange(5,15)];
- »ØÄꤷ¤¿ÈϰϤÎʸ»úÎó¤òºï½ü¤·¤Þ¤¹¡£