ダークホースプログラマー--NSStringの基礎知識

8137 ワード

可変文字列:初期化された文字列の内容は変更できませんが、文字列参照のアドレスは変更できます.
1)文字列を整数、単精度、二重精度、NSIntergerタイプデータに変換
    NSString *myintstr=@"3545";
        
    NSLog(@"mystr=%d",[myintstr intValue]);
        
    NSString *myfloatstr=@"35.45";
        
    NSLog(@"mystr=%.3f",[myfloatstr floatValue]);
    NSLog(@"mystr=%.4f",[myfloatstr doubleValue]);

出力:
2015-05-25 16:05:17.314試験問題7[3503:274398]myst=3545
2015-05-25 16:05:17.314試験問題7[353:274398]myst=35.450
2015-05-25 16:05:17.314試験問題7[3503:274398]mystr=35.4500
2)文字列初期化
 //        
        NSString *str11=[NSString string];//        
        NSLog(@"   :%@",str11);
        //            str1   
        res =[NSString stringWithString:str1];//       
        NSLog(@"   :%@",res);
        //                     
        NSString *str12=[NSString stringWithFormat:@"nihao"];
        NSLog(@"   :%@",str12);
        
        NSString *str13=[NSString stringWithFormat:@"%d",1321341];
        NSLog(@"   :%@",str13);

出力:
2015-05-25 16:34:32.735文字列練習[3576:283221]文字列:
2015-05-25 16:34:32.735文字列練習[3576:283221]文字列:This is string A
2015-05-25 16:34:32.735文字列練習[3576:283221]文字列:nihao
2015-05-25 16:34:32.36文字列練習[3576:283221]文字列:1321341
3)文字列に対する操作
a,文字列内の単一文字を取得する
[str1 characterAtIndex:i]   unichar   
  for (int i=0;i
出力:
2015-05-25 16:37:04.102文字列練習[3588:284140]0文字目は:T
2015-05-25 16:37:04.103文字列練習[3588:284140]1文字目は:h
2015-05-25 16:37:04.104文字列練習[3588:284140]2文字目は:i
2015-05-25 16:37:04.104文字列練習[3588:284140]3文字目は:s
2015-05-25 16:37:04.104文字列練習[3588:284140]4文字目:
2015-05-25 16:37:04.104文字列練習[3588:284140]5文字目は:i
2015-05-25 16:37:04.104文字列練習[3588:284140]6文字目は:s
2015-05-25 16:37:04.105文字列練習[3588:284140]7文字目:
2015-05-25 16:37:04.105文字列練習[3588:284140]8文字目は:s
2015-05-25 16:37:04.105文字列練習[3588:284140]9文字目:t
2015-05-25 16:37:04.105文字列練習[3588:284140]10文字目は:r
2015-05-25 16:37:04.105文字列練習[3588:284140]11文字目:i
2015-05-25 16:37:04.105文字列練習[3588:284140]12文字目:n
2015-05-25 16:37:04.106文字列練習[3588:284140]13文字目:g
2015-05-25 16:37:04.106文字列練習[3588:284140]14文字目:
2015-05-25 16:37:04.106文字列練習[3588:284140]15文字目は:A
b、先頭からiまでの文字列を返す
NSString *str20=[str1 substringToIndex:6];
        NSLog(@"       :%@",str20);

出力:
2015-05-25 16:43:43.804文字列練習[3609:285930]返される文字列は、This i
c)、i番目から末尾までの文字列を返す
NSString *str21=[str1 substringFromIndex:6];
        NSLog(@"       :%@",str21);

出力:
2015-05-25 16:46:17.150文字列練習[360:286828]返される文字列は:s string A
d)、指定範囲内の文字列を返す
NSMakeRange(3,7)/インデックス3から長さ7のすべてのオブジェクト文字を指す
//           
        NSString *str22=[str1 substringWithRange:NSMakeRange(3, 7)];
        NSLog(@"       :%@",str22);

出力:
2015-05-25 16:52:27.736文字列練習[3633:288491]返される文字列は:s is st
e)、文字列から文字列の一部を検索するインデックス
 NSRange subrange;//     
        subrange=[str1 rangeOfString:@"is s"];
        NSLog(@"        %lu 
is T %lu
%lu",str1,subrange.location,subrange.length);

出力:
2015-05-25 17:38:32.219文字列練習[3746:301080]文字列の位置4294971464の
is Tインデックスは5
検索された文字列は4です.
4)テスト文字列
        //         
        NSString *str30=[NSString stringWithString:str1];
        if ([str30 isEqualToString:str1]==YES)
        {
            NSLog(@"str30==str1");
        }
        else
        {
            NSLog(@"str30!=str1");
        }
        //       
        compareResult=[str30 compare:str1];//      compare,  isEqualToString
        if (compareResult==NSOrderedAscending)
        {
            NSLog(@"str30str1");

出力:
2015-05-25 17:10:50.110文字列練習[3668:293451]str 30=str 1
2015-05-25 17:10:50.12文字列練習[3668:293451]str 30=str 1
4)文字列の大文字と小文字の変換
  NSString *str40=[str1 uppercaseString];
        NSLog(@"     :%@",str40);
        
        NSString *str41=[str1 lowercaseString];
        
        NSLog(@"     :%@",str41);
出力:
2015-05-25 17:15:53.350文字列練習[3680:294905]大文字変換後:THIS IS STRING A
2015-05-25 17:15:53.351文字列練習[3680:294905]小文字変換後:this is string a
5)文字列を追加
注意:この方式はstr 1文字列が変更されたのではなく、両者を追加して新しい文字列を返す
 //       
        
        NSString *str50=[str1 stringByAppendingString:str2 ];
        NSLog(@"%@      :%@",str1,str50);
        
        str1=str50;
        NSLog(@"%@      :%@",str1,str50);

出力:
2015-05-25 17:23:51.720文字列練習[3696:297138]
This is string A追加文字列後:This is string AThis is string B
2015-05-25 17:23:51.721文字列練習[3696:297138]
This is string AThis is string B追加文字列後:This is string AThis is string B
可変文字列
a)部分文字列の挿入
//              
       
        mstr=[NSMutableString stringWithString:str1];
        NSLog(@"     :%@",mstr);
        
        //           
        [mstr insertString:@"mutable" atIndex:7];
         NSLog(@"     :%@",mstr);
        //            
        [mstr insertString:@"last" atIndex:[mstr length]];
        NSLog(@"     :%@",mstr);
        
        //             
        [mstr appendString:@" appendstring" ];
        NSLog(@"     :%@",mstr);
      
出力:
2015-05-25 20:49:15.057文字列練習[473:10550]可変文字列:This is string A
2015-05-25 20:49:15.058文字列練習[473:10550]可変文字列:This ismutable string A
2015-05-25 20:49:15.058文字列練習[473:10550]可変文字列:This ismutable string Alast
2015-05-25 20:49:15.058文字列練習[473:10550]可変文字列:This ismutable string Alast appendstring
b)指定範囲内の文字列を削除する
-(void)deleteCharactersInRange:range//range指定
NSMakeRangeは、インデックスとインデックスの文字列長を含む結果を返します.
mstr=[NSMutableString stringWithString:str1];
        NSLog(@"     :%@",mstr);
        [mstr deleteCharactersInRange:NSMakeRange(6,3)];//     6     3   
        NSLog(@"     :%@",mstr);

しゅつりょく
2015-05-25 20:46:24.82文字列練習[465:9635]可変文字列:This is string A
2015-05-25 20:46:24.84文字列練習[465:9635]可変文字列:This itringA
 mstr=[NSMutableString stringWithString:str1];
        NSLog(@"     :%@",mstr);
        substr =[str1 rangeOfString:@"string"];//       “string”   
        
        [mstr deleteCharactersInRange:substr];//     6     3   
        NSLog(@"     :%@",mstr);

出力:
2015-05-25 20:57:48.343文字列練習[485:12348]可変文字列:This is string A
2015-05-25 20:57:48.345文字列練習[485:12348]可変文字列:This is A
c)指定した文字列を特定の文字列に置き換える
 mstr=[NSMutableString stringWithString:str1];
        NSLog(@"     :%@",mstr);
        substr =[str1 rangeOfString:@"string"];//       “string”   
        
        [mstr replaceCharactersInRange:substr withString:@"hello word"];
        NSLog(@"     :%@",mstr);
文字の「string」は文字列「
ハローワード置換
出力:
2015-05-25 21:03:09.081文字列練習[504:13716]可変文字列:This is string A
2015-05-25 21:03:09.082文字列練習[504:13716]可変文字列:This is hello word A
d)すべての文字列を検索して置換する
   mstr=[NSMutableString stringWithString:str1];
        NSLog(@"     :%@",mstr);
        search=@"i";//       
        replace=@"I";//      
        substr =[mstr rangeOfString:@"i"];//       “a”   
        while (substr.location!=NSNotFound) {
            [mstr replaceCharactersInRange:substr withString:@"I"];//  
            substr =[mstr rangeOfString:@"i"];//    
        }
        NSLog(@"     :%@",mstr);

出力:
2015-05-25 21:13:14.587文字列練習[537:16093]可変文字列:This is string A
2015-05-25 21:13:14.588文字列練習[537:16093]可変文字列:ThIs strIng A