NSString

NSString:可以保存字符串。

创建NSString实例:

  1. NSString *lament = @"Why me!?";(并没有明确地发送消息给NSString类,@"..." 是Objective-C语言中的一个缩写,代表根据给定的字符串创建一个NSSTring对象,这种缩写称为字面量语法(literal syntax),该实例称为NSString的字面量实例(NSString字面量)。)
  2. 创建动态字符串:NSString *dateString = [NSString stringWithFormat:@"The date is %@",now];(类似于NSLog())
  3. NSString实例可以保存任意Unicode字符。如果需要插入非ASCII码字符,则可以使用\u,后面加上该字符的十六进制Unicode编码。如:NSString *slogan = @"I \u2661 New York!";

NSString常用方法:

  1. length方法:-(NSUInteger)length (获取字符串中字符的数量)。如:

    //-(NSUInteger)length
    NSUInteger charCount = [dateString length];
    
  2. isEqualToString:方法:-(Bool)isEqualToString:(NSString *)other; (查看一个字符串是否和另一个字符串相等)。如:

    //-(Bool)isEqualToString:(NSString *)other
    if([slogan isEqualToString:lament]) {
       NSLog(@"%@ and %@ are equal",slogan,lament);
    }
    
  3. uppercaseString方法:-(NSString *)uppercaseString;(把一个字符串变成大写形式)

    //-(NSString *)uppercaseString;
    NSString *angryText = @"That makes me so mad!";
    BSString *reallyAngryText = [angryText uppercaseString];
    
  4. 查找字符串

//- (NSRange)rangeOfString:(NSString *)searchString
NSString *listOfName = @"...";//名单
NSString *name = @"ward";
NSRange match = [listOfName rangeOfString:name];
if (match.location == NSNotFound) {
NSLog(@"no match found");
} else {
NSLog(@"match found");
}

5.  合并字符串
```js
//- (NSString *)stringByAppendingString:(NSString *)aString;
NSString *one = @"one and ";
NSString *two = @"two";
NSString *methodd = [one stringByAppendingString:two];
NSLog(@"%@",methodd);

你可能感兴趣的:(NSString)