iOS学习路之Objective-C(二)—— 字符串

  1. NSString对象的创建

     1     //创建不可变字符串
     2     NSString *string1 = @"bei jing huan ying nin";
     3 #pragma mark 对象方法创建字符串
     4     //用另一个字符串初始当前字符串,指针所指地址相同
     5     NSString *string2 = [[NSString alloc] initWithString:string1];
     6     //将其他的数据类型转化为字符串类型
     7     NSString *string3 = [[NSString alloc] initWithFormat:@"age is %d, name is %@", 23, @"DYK"];
     8 #pragma mark 类方法创建字符串
     9     //该方法与initWithString方法对应
    10     NSString *string4 = [NSString stringWithString:string1];
    11     //该方法与initWithFormat方法对应
    12     NSString *string5 = [NSString stringWithFormat:@"age is %d, name is %@", 23, @"DYK"];
  2. 字符串的拼接

    1     NSString *string1 = @"DYK";
    2     NSString *string2 = @"shuai";
    3     //- (NSString *)stringByAppendingString:(NSString *)aString;
    4     //作用:将两个字符串进行拼接
    5     //两个拼接的字符串都没有变,返回值是拼接后的字符串
    6     NSString *string3 = [string1 stringByAppendingString:string2];
    7     NSString *string4 = [NSString stringWithFormat:@"%@%@", string1, string2];

     

  3. 计算字符串的长度

    1     NSString *string = @"zhou dong yu";
    2     NSUInteger len = [string length];
  4. 取出字符串中成员的引用

    1     NSString *string = @"zhou dong yu";
    2     NSUInteger position = 0;
    3     char character = [string characterAtIndex:position];
  5. 字符串的比较

     1     NSString *string1 = @"chen chao";
     2     NSString *string2 = @"CHAN CHAO";
     3 #if 0
     4     //- (BOOL)isEqualToString:(NSString *)aString;
     5     //作用:比较两个字符串是否完全相同
     6     //BOOL:YES(真), NO(假)
     7     BOOL bl = [string1 isEqualToString:string2];
     8     if (bl) {
     9         NSLog(@"string1 == string2");
    10     } else{
    11         NSLog(@"string1 != string2");
    12     }
    13 #endif
    14     
    15 #if 0
    16     //- (NSComparisonResult)compare:(NSNumber *)decimalNumber;
    17     //作用:比较两个字符串是否相同(不忽略大小写)
    18     //NSComparisonResult:NSOrderedAscending(升序)
    19     //                   NSOrderedSame(相同)
    20     //                   NSOrderedDescending(降序)
    21     NSComparisonResult result = [string1 compare:string2];
    22     if (result == NSOrderedDescending) {
    23         NSLog(@"string1 < string2");
    24     } else if (result == NSOrderedAscending) {
    25         NSLog(@"string1 > string2");
    26     } else {
    27         NSLog(@"string1 == string2");
    28     }
    29 #endif
    30     //- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
    31     //作用:比较两个字符串是否相同(忽略大小写)
    32     NSComparisonResult result = [string1 caseInsensitiveCompare:string2];
    33     if (result == NSOrderedDescending) {
    34         NSLog(@"string1 < string2");
    35     } else if (result == NSOrderedAscending) {
    36         NSLog(@"string1 > string2");
    37     } else {
    38         NSLog(@"string1 == string2");
    39     }
  6. 在字符串中查找子串的位置

     1     NSString *string1 = @"chen chao zui shuai";
     2     NSString *string2 = @"zui";
     3     
     4     //- (NSRange)rangeOfString:(NSString *)aString;
     5     //作用:在一个字符串中查找子串的位置
     6     //typedef struct _NSRange {
     7     //    NSUInteger location;  起始位置
     8     //    NSUInteger length;    从起始位置开始的长度
     9     //} NSRange;
    10     NSRange range = [string1 rangeOfString:string2];
    11     if (range.location == NSNotFound) {
    12         NSLog(@"没有找到");
    13     }
    14     NSLog(@"location is %lu, length is %lu", range.location, range.length);

     

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。