1. initWithFormat : 初始化一个 NString 类型
- (instancetype)initWithString:(NSString *)aString // Returns an NSString object initialized by copying the characters from another given string.
例子:
int n = 17;
NSString * s = [[NSString alloc] initWithFormat:@"蓝欧%d班", n];
2. length : 得到NSString的长度
@property(readonly) NSUInteger length // The number of Unicode characters in the receiver. (read-only)
例子:
NSString * str = @"河南17班";
NSUInteger len = [str length];
NSLog(@"长度是 %ld", len);
3. stringByAppendingString :字符串拼接
- (NSString *)stringByAppendingString:(NSString *)aString //Returns a new string made by appending a given string to the receiver.
例子:
NSString * str = @"蓝鸥";
NSString * str2 = @"科技";
NSString * str3 = [str stringByAppendingString:str2];
NSLog(@"%@", str3);
4. stringByAppendingFormat : 字符串拼接 带参数
- (NSString *)stringByAppendingFormat:(NSString *)format , ... // Returns a string made by appending to the receiver a string constructed from a given format string and the following arguments.
例子:
int n =17;
NSString * str =@"蓝鸥";
NSString * newstr = [str stringByAppendingFormat:@"%d班", n];
NSLog(@"%@", newstr);
5. substringFromIndex : 得到某个字符从某个位数开始到结束
- (NSString *)substringFromIndex:(NSUInteger)anIndex //Returns a new string containing the characters of the receiver from the one at a given index to the end.
例子:
NSString * str = @"蓝鸥科技";
NSString * getString = [str substringFromIndex:2];
NSLog(@"%@", getString);
6. substringToIndex : 得到某段字符串从开始到某个位数结束
- (NSString *)substringToIndex:(NSUInteger)anIndex //Returns a new string containing the characters of the receiver up to, but not including, the one at a given index.
例子:
NSString * str = @"蓝鸥科技";
NSString * getString = [str substringToIndex:2];
NSLog(@"%@", getString);
7.substringWithRange : 得到某段字符串从给定的范围
- (NSString *)substringWithRange:(NSRange)aRange //Returns a string object containing the characters of the receiver that lie within a given range.
例子1 :
NSString * str = @"蓝鸥科技";
NSRange range = {1, 2}; //或 NSRange range = NSMakeRange(1, 2);
NSString * getString = [str substringWithRange:range];
例子2:
NSString * str = @"蓝鸥科技";
NSString * getString = [str substringWithRange:NSMakeRange(1, 2)];
NSLog(@"%@", getString);
8. componentsSeparatedByString 字符串分割
- (NSArray *)componentsSeparatedByString:(NSString *)separator // Returns an array containing substrings from the receiver that have been divided by a given separator.
例子:
NSString * str = @"www.baidu.com";
NSArray * subString = [str componentsSeparatedByString:@"."];
NSLog(@"%@", subString);
9. rangeOfString 求子串 在原字符串中的范围
- (NSRange)rangeOfString:(NSString *)aString // Finds and returns the range of the first occurrence of a given string within the receiver.
例子:
NSString * str = @"www.baidu.com";
NSRange s = [str rangeOfString:@"bai"];
NSLog(@"%lu %lu", s.location, s.length);
10.stringByReplacingOccurrencesOfString 替换元字符串中的字符 所有出现的某字符都替换成新字符
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
// Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
例子:
NSString * str = @"你是我的小呀小苹果";
NSString * s = [str stringByReplacingOccurrencesOfString:@"小" withString:@"大"];
NSLog(@"%@", s);
11.hasPrefix 判断员字符串是不是以 某字符串打头 返回Bool
- (BOOL)hasPrefix:(NSString *)aString // Returns a Boolean value that indicates whether a given string matches the beginning characters of the receiver.
例子:
NSString * str = @"http://www.baidu.com";
BOOL b = [str hasPrefix:@"http://"];
NSLog(@"%@", b ? @"YES" : @"NO");
12.hasSuffix 判断员字符串是不是以 某字符串结尾 返回Bool
- (BOOL)hasSuffix:(NSString *)aString //Returns a Boolean value that indicates whether a given string matches the ending characters of the receiver.
例子:
BOOL b1 = [str hasSuffix:@".com"];
NSLog(@"%@", b1 ? @"YES" : @"NO");
13.isEqualToString: 判断两个字符串是否相等
- (BOOL)isEqualToString:(NSString *)aString //Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.
例子:
NSString * str1 = @"蓝鸥科技";
NSString * str2 = @"蓝鸥科技";
BOOL b = [str1 isEqualToString:str2];
NSLog(@"%@", b ? @"YES" : @"NO");
14.lowercaseString 大写转小写
@property(readonly, copy) NSString *lowercaseString // A lowercase representation of the string.
例子
NSString * str1 = @"哈哈哈 hJJJkhjkJJJJ";
NSString * s = [str1 lowercaseString];
NSLog(@"%@", s);
15.uppercaseString 小写转大写
@property(readonly, copy) NSString *uppercaseString // An uppercase representation of the string. (read-only)
NSString * str1 = @"hJJJkhjkJJJJ";
NSString * s1 = [str1 uppercaseString];
NSLog(@"%@", s1);
16.capitalizedString 单词首字母大写
@property(readonly, copy) NSString *capitalizedString // A capitalized representation of the receiver. (read-only)
NSString * str = @"wo de xiao, yv ni xingle.";
NSString * s3 = [str capitalizedString];
NSLog(@"%@", s3);
17.integerValue floatValue doubleValue intvalue longlongValue boolValue字符串强制转换 函数
NSString * a = @"861.1";
NSString * b = @"657";
NSInteger num = [a integerValue] + [b integerValue];
NSLog(@"%ld", num);
CGFloat num1 = [a floatValue] + [b floatValue];
NSLog(@"%g", num1);