NSString其它常用方法

柴嘉石
2023-12-01



#pragma mark *** String searching ***

//字符串搜索

- (NSString *)commonPrefixWithString:(NSString *)str options:(NSStringCompareOptions)mask;//返回两个字符相同的开头部分


- (BOOL)containsString:(NSString *)str;//是否包含字符串

- (BOOL)localizedCaseInsensitiveContainsString:(NSString *)str;//以本地化不区分大小写方式判断是否包含字符串

- (BOOL)localizedStandardContainsString:(NSString *)str;

- (NSRange)localizedStandardRangeOfString:(NSString *)str;//如果包含此字符串,返回str字符串在字符串中的位置和长度


- (NSRange)rangeOfString:(NSString *)searchString;

- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;

- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;

- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(nullable NSLocale *)locale NS_AVAILABLE(10_5, 2_0);



#pragma mark *** Substrings ***


//字符串截取

- (NSString *)substringFromIndex:(NSUInteger)from;

- (NSString *)substringToIndex:(NSUInteger)to;

- (NSString *)substringWithRange:(NSRange)range;

- (void)getCharacters:(unichar *)buffer range:(NSRange)range;


    NSString *string1 = [NSString stringWithFormat:@"hello world!"];

    NSString *substring1FromIndex = [string1 substringFromIndex:2];

    NSLog(@"substring1FromIndex=%@",substring1FromIndex);//substring1FromIndex=llo world!

    NSString *substring1ToIndex = [string1 substringToIndex:4];

    NSLog(@"substring1ToIndex=%@",substring1ToIndex);//substring1ToIndex=hell

    NSString *substring1WithRange = [string1 substringWithRange:NSMakeRange(2, 5)];

    NSLog(@"substring1WithRange=%@",substring1WithRange);//substring1WithRange=llo w

    unichar char1[1000];

    [string1 getCharacters:char1 range:NSMakeRange(2, 5)];

    NSLog(@"%C",char1[0]);//l



#pragma mark *** Case changing ***

//大小写转换

@property (readonly, copy) NSString *uppercaseString;//全部大写

@property (readonly, copy) NSString *lowercaseString;//全部小写

@property (readonly, copy) NSString *capitalizedString;//首字母大写


@property (readonly, copy) NSString *localizedUppercaseString NS_AVAILABLE(10_11, 9_0);

@property (readonly, copy) NSString *localizedLowercaseString NS_AVAILABLE(10_11, 9_0);

@property (readonly, copy) NSString *localizedCapitalizedString NS_AVAILABLE(10_11, 9_0);


- (NSString *)uppercaseStringWithLocale:(nullable NSLocale *)locale NS_AVAILABLE(10_8, 6_0);

- (NSString *)lowercaseStringWithLocale:(nullable NSLocale *)locale NS_AVAILABLE(10_8, 6_0);

- (NSString *)capitalizedStringWithLocale:(nullable NSLocale *)locale NS_AVAILABLE(10_8, 6_0);

/*------一定不要使用 NSString  -uppercaseString  或者 -lowercaseString  的方法来处理 UI 显示的字符串,而应该使用 -uppercaseStringWithLocale  来代替------*/


//字符串补充字符串

- (NSString *)stringByAppendingString:(NSString *)aString;

- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

- (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;



//字符串替换

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;

- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement;



//写入URL或文件

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error;




  • NSString是以UTF-16 code uint的序列。所有的长度、字符、范围都是以16比特platform-endian(大端序小段序基于平台)的形式表示的。
    所以,一个
    英文字母的长度是1,一个汉字的长度也是1,而emoji表情的长度可能是2,3,4等等。可以在ASCII、UTF-8、UTF-16、UTF-32以及其他编码格式之间转换。
编码方式:

typedef NSUInteger NSStringEncoding;

NS_ENUM(NSStringEncoding) {

    NSASCIIStringEncoding = 1, /* 0..127 only */

    NSNEXTSTEPStringEncoding = 2,

    NSJapaneseEUCStringEncoding = 3,

    NSUTF8StringEncoding = 4,

    NSISOLatin1StringEncoding = 5,

    NSSymbolStringEncoding = 6,

    NSNonLossyASCIIStringEncoding = 7,

    NSShiftJISStringEncoding = 8,          /* kCFStringEncodingDOSJapanese */

    NSISOLatin2StringEncoding = 9,

    NSUnicodeStringEncoding = 10,

    NSWindowsCP1251StringEncoding = 11,    /* Cyrillic; same as AdobeStandardCyrillic */

    NSWindowsCP1252StringEncoding = 12,    /* WinLatin1 */

    NSWindowsCP1253StringEncoding = 13,    /* Greek */

    NSWindowsCP1254StringEncoding = 14,    /* Turkish */

    NSWindowsCP1250StringEncoding = 15,    /* WinLatin2 */

    NSISO2022JPStringEncoding = 21,        /* ISO 2022 Japanese encoding for e-mail */

    NSMacOSRomanStringEncoding = 30,


    NSUTF16StringEncoding = NSUnicodeStringEncoding,      /* An alias for NSUnicodeStringEncoding */


    NSUTF16BigEndianStringEncoding = 0x90000100,          /* NSUTF16StringEncoding encoding with explicit endianness specified */

    NSUTF16LittleEndianStringEncoding = 0x94000100,       /* NSUTF16StringEncoding encoding with explicit endianness specified */


    NSUTF32StringEncoding = 0x8c000100,                   

    NSUTF32BigEndianStringEncoding = 0x98000100,          /* NSUTF32StringEncoding encoding with explicit endianness specified */

    NSUTF32LittleEndianStringEncoding = 0x9c000100        /* NSUTF32StringEncoding encoding with explicit endianness specified */

};




 类似资料: