nsstring_nsstring常用方法 - CSDN
  • objective-c之NSString

    千次阅读 2012-04-12 15:16:12
    NSString在前面已经说过是,是Cocoa中用来处理字符串的类。下面就是NSString的一些常用方法及操作:  创建字符串 NSString的stringWithFormat:方法就是通过格式字符串和参数创建NSString的: + (id) ...

    NSString在前面已经说过是,是Cocoa中用来处理字符串的类。下面就是NSString的一些常用方法及操作:

     创建字符串

    NSString的stringWithFormat:方法就是通过格式字符串和参数创建NSString的:

    + (id) stringWithFormat: (NSString *) format,...;

    你可以按下面的使用,如:

    NSString *height;

    Height = [NSString stringWithFormat:@"your heightis %d feet", 5];

    那得到的字符串是“yourheight is 5 feet”

    类方法

    前面看到了stringWithFormat的定义,不知注意到了没有,和我们以前声明一个方法有两个很大的区别:

    第一个是参数用“,...”来结尾,这是表示它可以接多个参数;

    第二个是方法前面不是“-”,而是“+”,这是什么意思呢?这是表示它是一个类方法,这个方法属于类对象(而不是实例对象)并且通常用于创建新的实例。这种用来创建新对象的类方法称为工厂方法

     

    获取大小

    NSString中的另一个实例方法是length,它返回的是字符串中的字符个数。

    - (unsigned int) length;

    可以这样使用它:

    unsigned int len = [height length];

     

    比较的策略

    isEqualToString:可以用来比较接收方和当作参数传递来的符号。它返回一个BOOL型数据来表示两个字符串的内容是否相同。声明如下:

    - (BOOL) isEqualToString: (NSString *) aString;

    举例说明:

    NSString *string1 = @"hello 5";

    NSString *string2;

    string2 = [NSString stringWithFormat: @"hello%d",5];

    if([string1 isEqualToString:string2])

    {

        NSLog(@"They are same!");

    }

     

    compare:用来比较两个字符串,将接收对象和传递来的字符串进行比较,返回一个NSComparisonResult(就是一个enum型数据)来显示比较结果,声明如下:

    -(NSComparisonResult) compare:(NSString *) string;

    返回值的定义如下:

    typedef enum _NSComparisonResult{
         NSOrderedAscending = -1,    // <升序
         NSOrderedSame,              // =等于
         NSOrderedDescending  // >降序
    } NSComparisonResult;

    备注:如果比较两个字符串是否相等时,应该用isEqualToString,而不能仅仅只是比较字符串的指针值。另外compare判断的话只会根据第一个字符来进行判断,比如100与90,那就是升序,abcdef与dc比较,也是升序。

     

     不区分大小写的比较

    compare:也可以进行区分大小写的比较,它还有一种重载,多了一个option参数,用来给我们更多的选择与操作,方法具体定义如下:

    -(NSComparisonResult) compare:(NSString *) string

                          option:(unsigned)mask;

     

    options参数是一个位掩码,你可以使用位或运算符来添加选项标记,常用的选项如下:

    NSCaseInsensitiveSearch:不区分大小写字符。

    NSLiteralSearch:进行完全比较,区分大小写。

    NSNumericSearch:比较字符吕的字符个数,而不是字符值。

    例如:如果要进行字符串的比较,要忽略大小写但按字符个数的多少正确排序,那么应该如下使用:

    if([thing1compare:thing2

    option: NSCaseInsensitiveSearch

    | NSNumericSearch]

    == NSOrderedSame)

    {

        NSLog(@“they match!”);

    }

     

    如何判断字符串里面还包含其他字符串

    比如你要判断一个文件名里面是否包含“.avi”,这样可以判断是否可以用播放器打开它,再或者你想检查一个文件的开头是否包含了一个字符“my”,来判断它是否是你的文档。那么下面有两个方法来帮助进行判断:一个是检查字符串是否以另一个字符串进行开头,另一个是检查字符串是否以另一个字符串进行结尾:

    -(BOOL) hasPrefix:(NSString *) aString;

    -(BOOL) hasSuffix:(NSString *) aString;

    那么如何判断字符串中是否包含另一个字符串呢?还有另一个函数:

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

    返回的NSRange结构体,range.start为出现的第几个位置,range.length为要比较的字符串的长度,如果没有找到,则range.start则等于NSNotFound。


    可变的字符串

    NSString是不可变的,创建了之后就无法改变它,我们只能对它进行各种不影响其内容的操作,比如删除字符或者添加字符来改变它。所以Cocoa提供了一个NSString的子类,叫做NSMutableString,如果定义的字符串后面会发生改变,则使用这个子类。

    使用类方法stringWithCapacity:来创建一个新的NSMutableString,声明如下:

    - (id) stringWithCapacity: (unsigned) capacity:

    备注:capacity只是一个最优的容量,定义了这个大小后,并不是说仅限于这么大。

    NSMutableString *string;

    string = [NSMutableString stringWithCapacity: 42];

    NSMutableString常用的操作有如下:

    appendString:appendFormat:来附加新字符串,定义如下:

     - (void)appendString: (Nsstring *) aString;

     -  (void) appendFormat: (NSString *) Format;

    appendString 接受参数aString,然后将其复制到接受对象的末尾。

    appendFormat类似,他将格式化的字符串附加在接受对象的末尾,而不是创建新的对象。

    deleteCharacterInRange:用来删除字符串中的字符,定义如下:

    -(void)deleteCharactersInRange: (NSRange) range;

    通常将deleteCharactersInRange:和rangeOfString:连在一起使用,NSMutableString可以使用NSString的全部功能,包括rangeOfString:、字符串比较和其他任何功能。



    展开全文
  • 1、NSString和NSMutableStringNSString是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer2、测试demoint main(int argc, char * argv[]) { @autoreleasepool...

    1、NSString和NSMutableString

    NSString是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer



    2、测试demo

    int main(int argc, char * argv[]) {
        @autoreleasepool {
            unichar data[6] = {97, 98, 100, 101, 102, 103};
            NSString *str = [[NSString alloc] initWithCharacters:data length:6];
            NSLog(@"str is %@", str);
            char *cstr = "chenyu";
            NSString *str2 = [NSString stringWithUTF8String:cstr];
            NSLog(@"str2 is %@", str2);
    
            NSString *str3 = @"chenyu";
            NSString *name = @"陈喻";
            str3 = [str3 stringByAppendingString:@"chenyu"];
            NSLog(@"str3 is %@", str3);
            const char *cstr1 = [str3 UTF8String];
            NSLog(@"cstr1 is %s", cstr1);
            str3 = [str3 stringByAppendingFormat:@"hello %@ hello", name];
            NSLog(@"str3 is %@", str3);
            NSLog(@"str3 length is %lu", [str3 length]);
            NSString *s1 = [str3 substringToIndex:10];
            NSLog(@"s1 is %@", s1);
            NSString *s2 = [str3 substringFromIndex:5];
            NSLog(@"s2 is %@", s2);
            NSString *s3 = [str3 substringWithRange:NSMakeRange(5,10)];
            NSLog(@"s3 is %@", s3);
            NSRange pos = [str3 rangeOfString:@"陈喻"];
            NSLog(@"陈喻在str3中开始的位置:%ld,长度为%ld", pos.location, pos.length);
            str3 = [str3 uppercaseString];
            NSLog(@"str3 is %@", str3);
            NSMutableString *tstr = [NSMutableString stringWithString:@"hello"];
            [tstr appendString:@"chenyu"];
            NSLog(@"tstr is %@", tstr);
            [tstr appendFormat:@"hello word %@", @"chengongyu"];
            NSLog(@"tstr is %@", tstr);
            [tstr insertString:@"hello" atIndex:6];
            NSLog(@"tstr is %@", tstr);
            [tstr deleteCharactersInRange:NSMakeRange(6, 9)];
            NSLog(@"tstr is %@", tstr);
            [tstr replaceCharactersInRange:NSMakeRange(3, 6) withString:@"objectobject"];
            NSLog(@"tstr is %@", tstr);
        }
    }




    3、运行结果

    str is abdefg
    str2 is chenyu
    str3 is chenyuchenyu
    cstr1 is chenyuchenyu
    str3 is chenyuchenyuhello 陈喻 hello
    str3 length is 26
    s1 is chenyuchen
    s2 is uchenyuhello 陈喻 hello
    s3 is uchenyuhel
    陈喻在str3中开始的位置:18,长度为2
    str3 is CHENYUCHENYUHELLO 陈喻 HELLO
    tstr is hellochenyu
    tstr is hellochenyuhello word chengongyu
    tstr is hellochellohenyuhello word chengongyu
    tstr is hellocuhello word chengongyu
    tstr is helobjectobjectllo word chengongyu

    展开全文
  • NSString&NSMutableString常用操作梳理

    千次阅读 2019-06-30 16:34:35
    NSMutableArray常用操作,这次来梳理一下Objective-C中每天都要用到的字符串处理类——NSString。 Objective-C中的NSString/NSMutableString类似MFC中的CString,封装了字符串的日常基本操作。 1.创建初始化...

    上一篇梳理了NSArray&NSMutableArray常用操作,这次来梳理一下Objective-C中每天都要用到的字符串处理类——NSString。

    Objective-C中的NSString/NSMutableString类似MFC中的CString,封装了字符串的日常基本操作。

     

    1.创建初始化(Initialization&Creation

    1.1 常量字符串(literal string)

    NSString* constString = @"Hello, World!"; // 此处使用了字面量语法,否则需要使用alloc initWithUTF8String或stringWithUTF8String来从双引号的C String初始化NSString对象

     

    变量constString并不是真正包含一个字符串对象,而指向内存中字符串对象的指针(地址),我们称之为对象标识符

    以下示例中,字面量 @“Hello, World!”存储在文字常量区。指针constString1和constString2都指向它,它们在编译时生成的真实类型是NSConstantString(继承关系链——:NSSimpleCString:NSString)。

     

    Objective-C里没有包或者命名空间,靠前缀来区分,NS是“NeXTSTEP”的缩写。CF是“CoreFoundation”的缩写。CFString可以看做是NSString的C底层实现。

    Foundation库(Foundation.framework/Foundation.h)是有Cocoa框架提供的基本数据管理和服务功能的Objective-C接口,而Core Foundation库 (CoreFoundation.framework/CoreFoundation.h) 则是Cocoa底层实现,提供了C语言层面的接口。

     

    以下介绍不可变字符串两种初始化创建方法。

    After an immutable string has been initialized in the following way, it cannot be modified.

    1.2 Initializing an String(NS_DESIGNATED_INITIALIZER)

    - (instancetype)init NS_DESIGNATED_INITIALIZER;
    - (instancetype)initWithCoder:(NSCoder*)aDecoder NS_DESIGNATED_INITIALIZER;
     
    // 从UNICODE C String中选取定长字符初始化NSString对象
    - (instancetype)initWithCharacters:(const unichar*)characters length:(NSUInteger)length;
    // 从UTF8 C String初始化NSString对象
    - (instancetype)initWithUTF8String:(const char*)nullTerminatedCString;
    // 从C String初始化NSString对象,指定编码格式。
    - (instancetype)initWithCString:(const char*)nullTerminatedCString encoding:(NSStringEncoding)encoding;
     
    // 从另一个NSString初始化新的NSString对象
    - (instancetype)initWithString:(NSString*)aString;
    // 从NSData指定编码格式初始化NSString对象
    - (instancetype)initWithData:(NSData*)data encoding:(NSStringEncoding)encoding;
    // 从C Buffer指定编码格式初始化NSString对象
    - (instancetype)initWithBytes:(const void*)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
    // 可变参格式化初始化NSString对象
    - (instancetype)initWithFormat:(NSString*)format, ... NS_FORMAT_FUNCTION(1,2);

    比较常用的有以下几个:

    // UTF8 C String初始化NSString对象

    - (instancetype)initWithUTF8String:(constchar *)nullTerminatedCString;

    // C String初始化NSString对象,指定编码格式(例如NSUTF8StringEncoding)。

    - (instancetype)initWithCString:(constchar *)nullTerminatedCString encoding:(NSStringEncoding)encoding;

     

    // C Buffer指定编码格式初始化NSString对象

    - (instancetype)initWithBytes:(constvoid *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;

    // 可变参格式化初始化NSString对象

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

     

    示例:

        //NSString* string = [[NSString alloc] initWithFormat:@"Hello, %s","World!"];
        NSString* string = [[NSString alloc] initWithFormat:@"Hello, %@", @"World!"];
        NSLog(@"string= %@", string);

     

    关于格式化规范,参考String Format Specifiers

    • %@:OC对象描述(description)。
    • %d/%D:带符号32位整数(int):long:%ld;long long:%lld(或%qd);NSInteger:%zd(对应 size_t 长度的 signed integer)。
    • %u/%U:无符号32位整数(unsigned int),unsigned long:%lu;unsigned long long:%llu(或%qu);NSUInteger:%tu(对应 ptrdiff_t 长度的 signed integer)。
    • %o/%O:无符号32位整数(unsigned int)的八进制格式。
    • %x/%X:无符号32位整数(unsigned int)的十六进制格式,可采用 %#x 带 0x 前缀或自行添加 0x 前缀(0x%x)。
    • %hd/%hu:带/无符号16位短整型;%hhd/%hhu格式符则可打印有/无符号字符数值。
    • %c:8位无符号字符(unsigned char)。如果非ASCII码则以八进制“\\ddd”或十六进制“\\udddd”格式显示字符值。若要打印其数字,则应使用%hhu格式符。
    • %C:16位UNICODE字符(unichar)。如果非ASCII码则以八进制“\\ddd”或十六进制“\\udddd”格式显示字符值。
    • %f:64位浮点数(double)。
    • %p:打印带“0x”前缀的对象指针(地址)。如果不想带“0x”前缀,可使用%zx格式符。

    %lld,%llu、%zd,%tu 等中的 d/u 均可替换为 x 以十六进制打印。

    Xcode 中 'BOOL' aka 'signed char',建议以 %hhd 打印 BOOL 值。

    在 Xcode armv7/arm64 Toolchain 下,sizeof 运算符的返回值 size_t(__SIZE_TYPE__) 被定义为 long unsigned int(unsigned long),可以按 %lu 格式打印。

    1.3 Creating an String(autorelease)

    // 创建一个字符串独享,其值为空
    + (instancetype)string;

    示例:

        NSString* constString = @"";
        NSString* string = [NSString string];
        BOOL bPointerEqual = constString==string; // NO
        BOOL bContentEqualTo = [constString isEqualTo:string]; // YES
        BOOLbEqualToString = [constString isEqualToString:string]; // YES
    

     

    以下是一些便利构造方法:

    // initWithString对应的类方法
    + (instancetype)stringWithString:(NSString *)string;
    
    // initWithCString:encoding对应的类方法
    + (instancetype)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
    
    // initWithCharacters:length: 对应的类方法
    + (instancetype)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length;
    
    // initWithUTF8String对应的类方法
    + (instancetype)stringWithUTF8String:(const char *)nullTerminatedCString;
    
    // initWithFormat对应的类方法
    + (instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
    + (instancetype)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

    比较常用的有以下几个:

    // initWithCString:encoding对应的类方法

    + (instancetype)stringWithCString:(constchar *)cString encoding:(NSStringEncoding)enc;

     

    // initWithUTF8String对应的类方法

    + (instancetype)stringWithUTF8String:(constchar *)nullTerminatedCString;

    // initWithFormat对应的类方法

    + (instancetype)stringWithFormat:(NSString *)format, ...NS_FORMAT_FUNCTION(1,2);

     

    2.访问字符串(Querying

    2.1 字符串长度(字符数组大小)

    // Thenumber of Unicode characters in the receiver.
    @property (readonly) NSUInteger length;

    示例:

        NSString* constString1 = nil;
        NSString* constString2 = @"";
        NSString* constString3 = @"Hello, World!";
        NSString* constString4 = @"哈喽,世界!"; // 汉字+半角标点混合
       
        NSLog(@"constString1[size,length] = [%zd, %zd]", sizeof(constString1),constString1.length); // [8,0]
        NSLog(@"constString2[size,length] = [%zd, %zd]", sizeof(constString2),constString2.length); // [8,0]
        NSLog(@"constString3[size,length] = [%zd, %zd]", sizeof(constString3),constString3.length); // [8,13]
        NSLog(@"constString4[size,length] = [%zd, %zd]", sizeof(constString4),constString4.length); // [8,6]

    以上可知string.length可用来判空:如果length为零,则表示字符串对象为nil或为不包含任何字符。

    2.2 字符(字符数组元素)

    // 获取指定索引位置/范围的字符(集)
    - (unichar)characterAtIndex:(NSUInteger)index;
    - (void)getCharacters:(unichar *)buffer range:(NSRange)aRange;

     

    示例:

        unichar ch = [constString3 characterAtIndex:7];
        NSLog(@"ch = %c", ch); // W
        unichar* cBuf = malloc(sizeof(unichar)*constString3.length);
        [constString3 getCharacters:cBuf];
        NSString* stringFromCharacters1 = [[NSString alloc] initWithCharacters:cBuf length:constString3.length];
        NSLog(@"stringFromCharacters1 = %@", stringFromCharacters1); // @"Hello, World!"
        [stringFromCharacters1 release];
       
        [constString3 getCharacters:cBuf range:NSMakeRange(7, 6)];
        NSString* stringFromCharacters2 = [NSString stringWithCharacters:cBuf length:constString3.length];
        NSLog(@"stringFromCharacters2 = %@", stringFromCharacters2);  // @"World! World!"

    2.3 C String

    //Convenience to return null-terminated UTF8 representation
    @property (readonly) __strong const char *UTF8String NS_RETURNS_INNER_POINTER;

    2.4 取字面值

    类似cstdlib中的atoi/atol/strtol/atof。

    /* convenience methods all skip initial space characters (whitespaceSet)and ignore trailing characters. 
        <strong>NSScanner</strong> can be used for more"exact" parsing of numbers. */
    @property (readonly) double doubleValue;
    @property (readonly) float floatValue;
    @property (readonly) int intValue;
    @property (readonly) NSInteger integerValue NS_AVAILABLE(10_5, 2_0);
    @property (readonly) long long longLongValue NS_AVAILABLE(10_5, 2_0);
    @property (readonly) BOOL boolValue NS_AVAILABLE(10_5, 2_0);

    2.5 大小写转换

    // 所有字符转换成大写
    @property (readonly, copy) NSString *uppercaseString;
    // 所有字符转换成小写
    @property (readonly, copy) NSString *lowercaseString;
    // 每个单词首字母大写,其余字母小写。
    @property (readonly, copy) NSString *capitalizedString;

     

    3.查询字符串(Finding

    3.1 定位子串(rangeOf)

    // 返回查找到包含子串的范围
    - (NSRange)rangeOfString:(NSString *)aString;
    // 返回查找到包含子串的范围,可指定查询选项(忽略大小写、逆序)
    - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
    // 返回查找到包含子串的范围,可指定查询选项(忽略大小写、逆序),可进一步指定查找范围
    - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange;

    3.2 取子串(subString)

    // 返回从指定索引到结尾的子串
    - (NSString *)substringFromIndex:(NSUInteger)from;
    // 返回从开头到指定索引的子串
    - (NSString *)substringToIndex:(NSUInteger)to;
    // 返回从指定范围(开始索引+长度)界定的子串
    - (NSString *)substringWithRange:(NSRange)range;   // Hint: Use withrangeOfComposedCharacterSequencesForRange: to avoid breaking up composedcharacters

    示例:

        NSString* prefix = [constString3 substringToIndex:7]; // @"Hello, "
        NSString* suffix = [constString3 substringFromIndex:7]; // @"World!"
        NSString* substr =[constString3 substringWithRange:NSMakeRange(3,6)]; // @"lo, Wo"

    3.3 是否包含子串(prefix/suffix/contains)

    // 是否以指定子串开头
    - (BOOL)hasPrefix:(NSString *)aString;
    // 是否以指定子串结尾
    - (BOOL)hasSuffix:(NSString *)aString;
    // 是否包含子串,注意只适用于iOS8以上!
    - (BOOL)containsString:(NSString *)aString NS_AVAILABLE(10_10, 8_0);

    示例:

        BOOL bHasPrefix = [constString3 hasPrefix:@"Hello,"]; // YES
        BOOL bHasSuffix = [constString3 hasSuffix:@"World!"]; // YES
        BOOL bContain= [constString3 containsString:@"lo, Wo"]; // YES
    

    以下为NSString扩展类别(NSStringUtilities)判断是否包含子串的适配接口:

    @implementation NSString (NSStringUtilities)
    - (BOOL)hasContainStr:(NSString*)subStr
    {
        if(!subStr) {
            return NO;
        }
        
        if([self respondsToSelector:@selector(containsString:)]) { // ≥iOS8
            return [self containsString:subStr];
        } else { // <iOS8
            NSRange range = [self rangeOfString:subStr];
            return (range.location!=NSNotFound ? YES : NO); // return (range.length>0 ? YES : NO);
        }
    }
    @end

    3.4 查询字符集

    /* These return the range of the first character from the set in the string, not the range of a sequence of characters.
    */
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet;
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask;
    - (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange;
     
    - (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
    - (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0);
    

     

    4.比较字符串(Comparing

    // 判断两个字符串内容是否相等
    - (BOOL)isEqualToString:(NSString *)aString;
     
    /* In the compare: methods, the range argument specifies the subrange,rather than the whole, of the receiver to use in the comparison.
    The range is not applied to the search string.  For example, [@"AB"compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares"A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
    */
    // 比较字符串,如果相同返回NSOrderedSame;如果不相同,返回第一个不相同的字符值比较结果(NSOrderedAscending、NSOrderedDescending)
    - (NSComparisonResult)compare:(NSString *)string;
    // 比较字符串,可指定比较选项(忽略大小写、逆序、按十进制值)
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask;
    // 比较字符串,可指定比较选项(忽略大小写、逆序、按十进制值),可进一步指定查找范围
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange;
    // 比较字符串,可指定查询选项(忽略大小写、逆序、按十进制值),可进一步指定查找范围,可进一步按照本地化比较
    - (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; //locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice.
     
    // 相当于compare:string options:NSCaseInsensitiveSearch
    - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
    // 按照本地化比较
    - (NSComparisonResult)localizedCompare:(NSString *)string;
    // 以上两个版本组合
    - (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string; 

     

    5.替换子串(Replacing

    /* Replace all occurrences of the target string in the specified range with replacement. Specified compare options are used for matching target. IfNSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.*/
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0);
     
    /* Replace all occurrences of the target string with replacement. Invokes the above method with 0 options and range of the whole string.*/
    - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);
     
    /* Replace characters in range with the specified string, returning new string.*/
    - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0);

     

    6.衍生字符串(Deriving

    // 当前字符串追加aString生成返回一个新的NSString对象。
    - (NSString *)stringByAppendingString:(NSString *)aString;
    - (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

    7.分解字符串(Separate & Join Components)

    7.1 componentsSeparatedByString/componentsJointedByString

    // NSString::componentsSeparatedByString接口按照分割字符(子串)来切分字符串:NSString->NSArray
    - (NSArray *)componentsSeparatedByString:(NSString *)separator;
    - (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0);
    
    // NSArray::componentsJoinedByString接口将多个字符串(字符串数组)以衔接字符连接:NSArray->NSString
    - (NSString *)componentsJoinedByString:(NSString *)separator;

    以下代码示例对典型的本地文件路径字符串按分隔符进行拆分分解,然后再按分隔符衔接复原:

        NSString* originString = @"do-do-sol-sol-la-la-sol";
        NSArray* separatedComponents = [originString componentsSeparatedByString:@"-"]; // 拆分
        NSLog(@"separatedComponents = %@", separatedComponents);
        NSString* jointedString = [separatedComponents componentsJoinedByString:@"-"]; //复原
        NSLog(@"jointedString = %@", jointedString);

    7.2 NSPathUtilities

    NSPathUtilities中NSString (NSStringPathExtensions)扩展提供了pathComponentslastPathComponentpathExtension属性用于分析文件目录/路径字符串(Path String)。

        // Mac文件系统目录路径
        NSString* pdWin8VMPath = @"/Users/faner/VM/Windows 8.1.pvm";
        // 拆分文件路径字符串
        NSArray* pathComponents = [pdWin8VMPath componentsSeparatedByString:@"/"];
        NSLog(@"separatedComponents = %@", pathComponents); // {@"",@"Users",@"faner",@"VM",@"Windows 8.1.pvm"}
        // 分析文件路径字符串
        NSString* lastPathComponent = pathComponents.lastObject; // full file name
        NSArray* lastPathSeparatedComponents = [lastPathComponent componentsSeparatedByString:@"."];
        NSString* fileName = lastPathSeparatedComponents.firstObject; // file name
        NSString* pathExtension = lastPathSeparatedComponents.lastObject; // file extension
        NSLog(@"pdWin8VMPath.fileName/pathExtension = {%@, %@}", fileName, pathExtension); // {Windows 8, pvm}
        // 复原文件路径字符串
        NSString* jointedString = [pathComponents componentsJoinedByString:@"/"];
        NSLog(@"jointedString = %@", jointedString);
        
        // NSPathUtilities.h
        NSLog(@"pdWin8VMPath.absolutePath = %d", pdWin8VMPath.absolutePath); // 1(是绝对文件路径)
        NSLog(@"pdWin8VMPath.pathComponents = %@", pdWin8VMPath.pathComponents); // {@"/",@"Users",@"faner",@"VM",@"Windows 8.1.pvm"}
        NSLog(@"pdWin8VMPath.lastPathComponents = %@", pdWin8VMPath.lastPathComponent); // Windows 8.1.pvm
        NSLog(@"pdWin8VMPath.pathExtension = %@", pdWin8VMPath.pathExtension); // pvm

    注意:不同于普通的字符串,Mac绝对文件路径往往以‘/’开头,代表文件系统根节点(File System Root),故文件路径字符串中的pathComponents不能简单等同于[path componentsSeparatedByString:@"/"]!

    字符串处理经常涉及到的资源路径和超链接路径的处理,它们和文件系统目录路径的构成稍有不同,一般由scheme(protocol)+resourceSpecifier构成。

    本地文件系统的scheme是“file”,以下基于文件路径构建resource URL(追加"file://"前缀):

        // 基于文件路径字符串构建资源URL,例如用于共享的URL Scheme
        NSURL* pdWin8VMURL = [NSURL fileURLWithPath:pdWin8VMPath];
        NSLog(@"pdWin8VMURL = %@", pdWin8VMURL); // pdWin8VMURL.absoluteString = file:///Users/faner/VM/Windows%208.1.pvm/

    构造NSURL

    使用NSString相关格式化接口可以拼接构造URL,NSPathUtilities中NSString (NSStringPathExtensions)扩展提供的接口(stringByAppendingPathComponent/stringByDeletingLastPathComponentstringByAppendingPathExtension/stringByDeletingPathExtension)也可辅助拼接构造URL。

    当然,NSURL类提供了更直接的便利初始化函数(Convenience initializers),支持直接基于NSString初始化创建NSURL。

    7.3 NSURLPathUtilities

    字符串处理经常涉及到的资源路径和超链接路径的处理,它们和文件系统目录路径的构成稍有不同,一般由scheme(protocol)+resourceSpecifier构成。WWW网站地址HTTP URL的scheme为“http”、“https”,host后的第一个‘/’代表网站根目录(WebRoot)。

    NSURL.h中的NSURL (NSURLPathUtilities)扩展也提供了pathComponentslastPathComponentpathExtension属性用于URL路径字符串。

    沿袭上面的例子,下面以两个小例程来分别阐述本地资源路径(asset URL)和超链接路径(HTTP URL)的典型处理。

    NSURLPathUtilities Demo1:iOS相册文件asset URL

        // demo1:iOS相册文件assetURL
        NSString* iOSDCIMAssetPath = @"file:///var/mobile/Media/DCIM/101APPLE/IMG_1354.JPG";
        NSURL* iOSDCIMAssetURL = [NSURL URLWithString:iOSDCIMAssetPath];
        // absoluteString&relativeString
        NSLog(@"iOSDCIMAssetURL.absoluteString = %@", iOSDCIMAssetURL.absoluteString);
        NSLog(@"iOSDCIMAssetURL.relativeString = %@", iOSDCIMAssetURL.relativeString);
        // host&port = {(null), (null)}
        NSLog(@"iOSDCIMAssetURL.host/port = {%@, %@}", iOSDCIMAssetURL.host, iOSDCIMAssetURL.port);
        // scheme&resourceSpecifier
        NSLog(@"iOSDCIMAssetURL.scheme = %@", iOSDCIMAssetURL.scheme); // file
        NSLog(@"iOSDCIMAssetURL.resourceSpecifier = %@", iOSDCIMAssetURL.resourceSpecifier); // /var/mobile/Media/DCIM/101APPLE/IMG_1354.JPG
        // path&relativePath
        NSLog(@"iOSDCIMAssetURL.path/relativePath = {%@, %@}", iOSDCIMAssetURL.path, iOSDCIMAssetURL.relativePath); // 同resourceSpecifier
        // pathComponents = {"/", "var", "mobile", "Media", "DCIM", "101APPLE", "IMG_1354.JPG"}
        NSLog(@"iOSDCIMAssetURL.pathComponents = %@", iOSDCIMAssetURL.pathComponents);
        // lastPathComponent = pathComponents.lastObject; lastPathComponent/pathExtension = {IMG_1354.JPG, JPG}
        NSLog(@"iOSDCIMAssetURL.lastPathComponent/pathExtension = {%@, %@}", iOSDCIMAssetURL.lastPathComponent, iOSDCIMAssetURL.pathExtension);

    说明:relativePath是相对baseURL而言,一般baseURL为nil,则relativePath等于path。


    NSURLPathUtilities Demo2:知乎搜索“大圣归来”的HTTP URL

        // demo2:知乎搜索“大圣归来”的HTTP URL
        NSString* zhihuSearchHttpURLPath = @"http://www.zhihu.com:80/search?q=%E5%A4%A7%E5%9C%A3%E5%BD%92%E6%9D%A5&type=question"; // 汉字部分已经使用CFURLCreateStringByAddingPercentEscapes进行URLEncode。
        NSURL* zhihuSearchHttpURL = [NSURL URLWithString:zhihuSearchHttpURLPath];
        // absoluteString&relativeString
        NSLog(@"zhihuSearchHttpURL.absoluteString = %@", zhihuSearchHttpURL.absoluteString);
        NSLog(@"zhihuSearchHttpURL.relativeString = %@", zhihuSearchHttpURL.relativeString);
        // host&port = {www.zhihu.com, 80}
        NSLog(@"zhihuSearchHttpURL.host/port = {%@, %@}", zhihuSearchHttpURL.host, zhihuSearchHttpURL.port);
        // scheme&resourceSpecifier = [url componentsSeparatedByString:@":"]
        NSLog(@"zhihuSearchHttpURL.scheme = %@", zhihuSearchHttpURL.scheme); // http
        NSLog(@"zhihuSearchHttpURL.resourceSpecifier = %@", zhihuSearchHttpURL.resourceSpecifier); // 协议冒号(不包括)之后双斜杠开始至末尾
        // path&relativePath = {/search, /search}
        NSLog(@"zhihuSearchHttpURL.path/relativePath = {%@, %@}", zhihuSearchHttpURL.path, zhihuSearchHttpURL.relativePath);
        // pathComponents = {"/","search"}
        NSLog(@"zhihuSearchHttpURL.pathComponents = %@", zhihuSearchHttpURL.pathComponents);
        // lastPathComponent = pathComponents.lastObject
        NSLog(@"zhihuSearchHttpURL.lastPathComponent = %@", zhihuSearchHttpURL.lastPathComponent);
        // fragment/query,其中query为问号之后的部分
        NSLog(@"zhihuSearchHttpURL.fragment/query = {%@, %@}", zhihuSearchHttpURL.fragment, zhihuSearchHttpURL.query); // {(null), q=%E5%A4%A7%E5%9C%A3%E5%BD%92%E6%9D%A5&type=question}
        // parameterString(RFC 1808)
        NSLog(@"zhihuSearchHttpURL.parameterString = %@", zhihuSearchHttpURL.parameterString); // (null)

    在处理URL时,经常要解析提取query items。主要有两种途径析取:

    (1)基于分隔符‘&’和‘=’对字符串进行分解提取。

        // 解析查询字符串的key-value(parseComponentsFromQueryString)
        NSString* queryString = zhihuSearchHttpURL.query;
        NSMutableDictionary *queryDict = [NSMutableDictionary dictionary];
        if (queryString.length) {
            NSArray *kvComponents = [queryString componentsSeparatedByString:@"&"];
            for (NSString *kvComponent in kvComponents) {
                NSString *key, *value;
                
                if ([kvComponent hasContainStr:@"="]) {
                    NSArray* KV = [kvComponent componentsSeparatedByString:@"="];
                    key = KV.firstObject;
                    value = KV.lastObject;
                } else {
                    key = kvComponent;
                    value = @"";
                }
                
                // 暂未考虑重名参数:k={v1,v2,...}
                [queryDict setObject:value forKey:key];
            }
        }
        // {q = "%E5%A4%A7%E5%9C%A3%E5%BD%92%E6%9D%A5"; type = question;}
        NSLog(@"parseComponentsFromQueryString = %@", queryDict);

     

    (2)基于iOS7提供的新接口,基于URLString构造NSURLComponents,自动提取queryItems(NSURLQueryItem[])。

        // NS_CLASS_AVAILABLE(10_9, 7_0)提供了NSURLComponents;NS_CLASS_AVAILABLE(10_10, 8_0)进一步提供了NSURLQueryItem
        NSURLComponents* zhihuSearchHttpURLComponents =  [NSURLComponents componentsWithString:zhihuSearchHttpURLPath];
        NSLog(@"zhihuSearchHttpURLComponents.queryItems = %@", zhihuSearchHttpURLComponents.queryItems); // array of NSURLQueryItem

     

    8.解析字符串NSScanner

    8.1 NSNumber

    可使用 NSNumberFormatter 与 NSString 之间进行便捷转换。

    // Even though NSNumberFormatter responds to the usual NSFormatter methods,
    //   here are some convenience methods which are a little more obvious.
    - (nullable NSString *)stringFromNumber:(NSNumber *)number;
    - (nullable NSNumber *)numberFromString:(NSString *)string;

     

    8.2 NSScanner

    NSString 提供了 intValue 等解析数值的接口(Extracting numeric values),还可利用 NSScannersscanf) 进行析取数值

    @interface NSScanner : NSObject <NSCopying>
    
    @property (readonly, copy) NSString *string;
    @property NSUInteger scanLocation;
    @property (nullable, copy) NSCharacterSet *charactersToBeSkipped;
    @property BOOL caseSensitive;
    
    - (instancetype)initWithString:(NSString *)string NS_DESIGNATED_INITIALIZER;
    
    @end
    
    @interface NSScanner (NSExtendedScanner)
    
    // On overflow, the below methods will return success and clamp
    - (BOOL)scanInt:(nullable int *)result;
    - (BOOL)scanLongLong:(nullable long long *)result;
    - (BOOL)scanHexInt:(nullable unsigned *)result; // Optionally prefixed with "0x" or "0X"
    - (BOOL)scanHexLongLong:(nullable unsigned long long *)result NS_AVAILABLE(10_5, 2_0); // Optionally prefixed with "0x" or "0X"
    
    @property (getter=isAtEnd, readonly) BOOL atEnd;
    
    + (instancetype)scannerWithString:(NSString *)string;
    
    @end

     

    9.可变字符串(NSMutableString

    9.1 Initializing an String(NS_DESIGNATED_INITIALIZER)

    /* In addition to these two, NSMutableString responds properly to allNSString creation methods.*/
    - (NSMutableString *)initWithCapacity:(NSUInteger)capacity;
    + (NSMutableString *)stringWithCapacity:(NSUInteger)capacity;

    9.2 set/append aString

    // Replaces the characters of the receiver with those in a given string.aString must not be nil.
    - (void)setString:(NSString *)aString;

     

    setString类似于对retain propery的赋值(setter)。

    字符串置为空串:=@””或setString:@””;

    // Adds to the end of the receiver the characters of a given string.aString must not be nil
    - (void)appendString:(NSString *)aString;
    - (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);

    9.3 insert/replace

    - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;
     
    // 替换
    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;
    - (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;

    9.4 delete

    // 删除指定位置、指定长度的子串
    - (void)deleteCharactersInRange:(NSRange)range;
    

     

    10.文件、URL与字符串

    10.1 从指定文件读取内容到字符串

    - (instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;
    + (instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

    10.2 从指定url读取(下载)内容到字符串

    - (instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;
    + (instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;

    10.3 将字符串内容写到指定url/path

    /* Write to specified url or path using the specified encoding.*/
    - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFileencoding:(NSStringEncoding)encerror:(NSError **)error;
    - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFileencoding:(NSStringEncoding)encerror:(NSError **)error;
    

     

    11.对象字符串化

    11.1 NSString From CGPoint/CGSize/CGRect

    NSStringFromCGPoint/CGPointFromStringNSStringFromCGSize/CGSizeFromString NSStringFromCGRect/CGRectFromString

    可用于对C复合类型(struct)进行OC描述(description),生成对应的字符串,方便NSLog调试输出。

    11.2 NSString From Class

    // 获取某个对象的具体类名,传入[obj class],相当于[obj className]
    FOUNDATION_EXPORT NSString *NSStringFromClass(Class aClass);
     
    // 基于类名获取类
    FOUNDATION_EXPORT Class NSClassFromString(NSString *aClassName);

    示例:

        NSLog(@"NSStringFromClass([constString2 class]) = %@", NSStringFromClass([constString2 class])); //__NSCFConstantString
    

    以下为Xcode的Single View Application模板的main.m代码,其中UIApplicationMain函数的第4个参数需要指定delegateClassName

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    
    int main(int argc, char * argv[]) {
        @autoreleasepool {
            return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
    }

     

    类似的还有NSStringFromSelector,以下为调试打印类的元数据信息示例:

    #import<Foundation/Foundation.h>
    #import <objc/runtime.h>// class_getName
     
    @implementation NSString (NSStringDebug)
    - (void)debugTrace
    {
        // __FILE__ = main.m
        NSLog(@"__FILE__.fileName = %s", ((strrchr(__FILE__,'/')?:__FILE__-1)+1));
        NSLog(@"__FILE__.lastPathComponent =%@", [[NSStringstringWithUTF8String:__FILE__]lastPathComponent]);
       
        //__func__,__FUNCTION__ = -[NSString(NSStringDebug) debugTrace]
        NSLog(@"__PRETTY_FUNCTION__ = %s", __PRETTY_FUNCTION__);
       
        // className = __NSCFString
        NSLog(@"self.className = %@",self.className); // NSClassDescription.h
        NSLog(@"object_getClassName(self) = %s",object_getClassName(self));// objc/runtime.h
        NSLog(@"class_getName([self class]) =%s",class_getName([self class]));// objc/runtime.h
        NSLog(@"NSStringFromClass([self class]) =%@",NSStringFromClass([self class]));// NSObjCRuntime.h
       
        // _cmd = debugTrace
        NSLog(@"NSStringFromSelector(_cmd) =%@",NSStringFromSelector(_cmd)); // NSObjCRuntime.h
    }
    @end
     
    int main(int argc,constchar * argv[]) {
        @autoreleasepool {
            //insert code here...
            NSString* stringEx = [NSString stringWithFormat:@"stringEx::debugTrace"];
            [stringEx debugTrace];
        }
       
        return0;
    }

     

    参考:《gcc 預先定義的巨集》《利用小技巧用于跟踪函数的进出过程》《Objective-C中的Debug表达式

     

    11.3 Class & SEL from NSString

    可通过字符串创建Class、获取Selector(SEL),然后调用performSelector实现动态调用(反射)。

    以下代码片段中,hideAlertView接口试图dismiss当前顶层UIAlertView/UIAlertController,在iOS 7.x系统中调用半公开的_UIAlertManager::topMostAlert获取顶层UIAlertView。

    - (void)hideAlertView
    {
        if (SYSTEM_VERSION < 7.0) { // iOS 7.0之前采用UIAlertView
            for (UIWindow* w in [UIApplication sharedApplication].windows) {
                for (NSObject* o in w.subviews) {
                    if ([o isKindOfClass:[UIAlertView class]]) {
                        UIAlertView *alert = (UIAlertView*)o;
                        [alert dismissWithClickedButtonIndex:alert.cancelButtonIndex animated:NO];
                    }
                }
            }
        } else if (SYSTEM_VERSION < 8.0) { // iOS 8.0之前采用UIAlertView
            Class UIAlertManager = NSClassFromString(@"_UIAlertManager");
            SEL selTopMostAlert = NSSelectorFromString(@"topMostAlert");
            if ([UIAlertManager respondsToSelector:selTopMostAlert]) {
                 UIAlertView *topMostAlert = [UIAlertManager performSelector:selTopMostAlert];
                 [topMostAlert dismissWithClickedButtonIndex:topMostAlert.cancelButtonIndex animated:NO];
            }
        } else { // iOS 8.0之后采用UIAlertController
            UINavigationController *navCtrl = self.navigationController;
            if(navCtrl.presentedViewController && [navCtrl.presentedViewController isKindOfClass:[UIAlertController class]]) {
                [navCtrl.presentedViewController dismissViewControllerAnimated:NO completion:nil];
            }
        }
    }

     

    注意:以上代码中使用NSClassFromString、NSSelectorFromString涉及到的私有接口(_UIAlertManager::topMostAlert)可能审核不通过。打散格式化组合在一定程度上可以规避苹果的静态扫描检查。

     

    参考:

     

    Objective-C研究院之NSString字符串(三)

    Objective-C研究院之NSMutableString(四)
     

    NSString &Unicode》《遍历NSString每一个字符的正确方式

    NSString属性用copy还是用strong?》《Use copy for NSStringproperties

     

    Objective-c中 isEqual ,isEqualToString , == 三者的区别

    NSString: isEqual vs.isEqualToString》《Equality》《isEqual: vsisEqualToString:

     

    How to convert anNSString to hex values

    NSString转换成16进制》《Objective-C NSStringto Hex

    How to convertNSString to hexadecimal string of fixed block size

    展开全文
  • Objective-C中字符串(NSString和NSMutableString)常用的方法标签(空格分隔): 字符串常用方法一、NSString的常用方法(不可变字符串)1、字符串实例化1)直接赋值NSString *value = @"lanou"; NSString *all = @...

    Objective-C中字符串(NSString和NSMutableString)常用的方法

    标签(空格分隔): 字符串常用方法


    一、NSString的常用方法(不可变字符串)

    1、字符串实例化

    1)直接赋值

    NSString *value = @"lanou";
    NSString *all = @"lanou3g is very important!";

    2)通过initWithFormat:

    NSString *value_3 = [[NSString alloc] initWithFormat:@"%d %c",2,'t'];

    便利构造器

    NSString *string = [NSString stringWithFormat:@"%d %c",2,'t'];

    2、获取字符串长度

    @property (readonly) NSUInteger length;

    NSLog(@"length = %ld",[all length];

    3、获取字符串中字符

    - (unichar)characterAtIndex:(NSUInteger)index;

    unichar c = [all characterAtIdex:3];

    4、字符串的比较方法

    NSString *value = @"abc";
    NSString *value = @"ABC";

    1)判断两个字符串对象是否相等

    - (BOOL)isEqualToString:(NSString *)aString;

    BOOL isEqual = [value1 isEqualToString:value2];
    • 结果是0说明不相等,结果是1,说明相等

    2)判断两个字符串对象的大小

    - (NSComparisonResult)compare:(NSString *)string;

    NSComparisonResult result = [value1 compare:value2];
    • value1和value2 升序排序结果是-1,相等是0,降序排序是1

    5、获取字符串的子串,以及字符串中的某个字符

    1)获取子串——从输入的数字开始到最后

    - (NSString *)substringFromIndex:(NSUInteger)from

    NSString *substringFromIndex = [all substringFromIndex:3];

    2)获取子串——从输入的数字之前

    - (NSString *)substringToIndex:(NSUInteger)to

    NSString *substringToIndex = [all substringToIndex:3];

    3)声明一个变量用来指向获取的子串对象

    - (NSString *)substringWithRange:(NSRange)range

    // 声明一个变量  范围变量
    // 1、定义范围的起点
    // 2、定义范围的长度
    NSRange range = NSMakeRange(8,2);
    // 获取is字符串
    NSString *subString = [all substringWithRange:range];
    // 一般写成NSString *subString = [all substringWithRange:NSMakeRange(8,2)];

    6、字符串链接(拼接)

    NSString *value_1 = @"lanou";
    NSString *value_2 = @"3g";

    1)声明一个结果字符串对象

    NSString *result = nul;
    result = [value_1 stringByAppendingString:value_2];

    2)声明一个结果字符串对象

    *- (NSString )stringByAppendingFormat:(
    NSString )format, …;*

    NSString *result_2 = nul;
    result_2 = [value_2 stringByAppendingFormat:@" %@",value_1];
    • 在对字符串对象发送stringByAppending类型的方法的时候,该字符串对象是不会发生变化的。

    7、替换字符串

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

    NSString *str = [all stringByplacingCharactersInRange:NSMakeRange(1,3) withString:@"xxx"];

    8、字符串与其他数据类型进行转换

    @property (readonly) int intValue;

    • 其他类型的用法与其相同
    NSString *value = @"2";
    int intValue = [value intValue];
    • 其中的int intValue可以替换为double doubleValue或者float floatValue

    9、字符串改变大小写

    1)字符串全部大写

    @property(readonly,copy)NSString *uppercaseString;

    NSString *upper = [all uppercaseString];

    2)字符串全部小写

    @property(readonly,copy)NSString *lowercaseString;

    NSString *lower = [all lowercaseString];

    3)字符串首字母大写

    @property(readonly,copy)NSString *capitalizedString;

    NSString *capital = [all capitalizedString];

    10、字符串的前后缀

    1)是否以指定字符串为前缀

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

    BOOL b1 = [all hasPrefix:@"m"]; // 判断是否以字符m开头

    2)是否以指定字符串为后缀

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

    BOOL b2 = [all hasSuffix:@"h"]; // 判断是否以字符h结尾

    二、NSMutableString的常用方法(可变字符串)

    NSMutableString *strm = [NSMutableString stringWithCapacity:100];

    1、拼接字符串

    - (void)appendFormat:(NSString *)format, …

    [strm appendFormat:@"Lan"];

    2、插入字符串

    - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc

    [strm insertString:@"a" atIndex:1];

    3、删除字符串

    - (void)deleteCharactersInRange:(NSRange)range

    [strm deleteCharactersInRange:NSMakeRange(1, 1)];

    4、替换字符串

    - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString

    [strm replaceCharactersInRange:NSMakeRange(2, 1) withString:@"m"];

    5、重置字符串

    - (void)setString:(NSString *)aString;

    [strm setString:@"hao"];
    展开全文
  • 字符串 stringWitString / initWithFormat(按照一定的格式拼接字符串 字符串里面的值由一些变量决定) stringWithFormat(用已经存在的字符串创建一个新的字符串) substringToInde(查找子字符串从开头 -&...
  • NSString是定义固定大小的字符串,NSMutableString可变,可对字符串做增、删、改、插入等操作。 源代码strings.m ----------------------------------- #import int main (int argc, const
  • NSString用法总结

    2019-05-11 23:18:20
    写在开头 最近踩了多个string的坑,嗯嗯!还是要抽时间总结一下的,顺便在后面写几个自己写的或者学习别人的算法 1、NSString的创建 ...NSString * string_1 = [NSString string]; string...
  • NSString类详解

    千次阅读 2017-05-05 16:52:56
    说到字符串NSString肯定每个开发IOS的都用过,它是Foundation框架下的一个核心处理字符串的类,掌握它非常有用。 属性: 最直接的属性有length,字符串长度 2.字符串的获取 //截取指定位置之后的字符串 - (NSString *...
  • NSString *str1 = [NSString stringWithFormat:@"小强"]; NSString *str2 = [NSString stringWithFormat:@"1"]; NSString *str3 = [NSString stringWithFormat:@"a"]; NSString *str4...
  • iOS NSString详解

    2018-11-19 19:00:41
    一:__NSCFConstantString __NSCFString NSTaggedPointerString 二:weak修饰,字符串内存管理 三:NSTaggedPointerString讲解 四:面试题 ...OC中的NSString不论是在编译时还是在运行时都做了很多...
  • stackOverflow上说法是为了防止mutable string被无意中修改, NSMutableString是NSString的子类, 因此NSString指针可以持有NSMutableString对象. 用代码解释就是 @property (nonatomic, strong) ...
  • NSString字符串截取三种方法

    万次阅读 2014-01-14 14:28:51
    1.定义一个字符串a, ... NSString *a = @"1.2.30";  int b= [[a substringWithRange:NSMakeRange(4,2)] intValue];   NSLog(@"a:%@ \n",a );  NSLog(@"b:%d",b );   O
  • NSString拼接字符串

    万次阅读 2012-05-02 09:57:41
    NSString* string; // 结果字符串 02 NSString* string1, string2; //已存在的字符串,需要将string1和string2连接起来 03   04 //...
  • NSString转换成UTF8编码的NSString

    万次阅读 2013-08-07 15:41:22
    在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数:  NSString *urlString=... NSString * encodedString = (NSString *)CF
  • 解决NSData转NSString返回nil的问题

    万次阅读 2014-11-09 02:17:43
    在使用initWithData等方法将NSData转换成NSString时,如果NSData的内容含有非encoding编码的字符,将会返回nil。 这个结果在很多时候可能并不是我们所希望的,比如在获取网页源码进行分析等方面,如果页面采用了utf-...
  • OC下NSString转ASCII.ASCII转NSString

    千次阅读 2018-06-23 16:49:16
    打个比方.我现在想把对应的0.1.2.3.4等转成A.B.C.D....//比如现在有一个NSString:@&quot;0&quot; NSString *str = @&quot;0&quot;; //1.转换成const char*类型.这里得出的是char*指针 ...
  • 前言:写这篇文章的初衷,是对阅读别人的博客提出的疑问,一路探索得来的。同时也要加强对内存管理以及... __weak NSString *str = [[NSString alloc] initWithFormat:@"1234"];  NSLog(@"%@", str);
  • 将 NSURL 转换为 NSString

    2018-10-30 23:53:00
    <p>Now in the case of the built-in app images I do get the file name as an <code>NSString</code> an save in the [occasion imagePath]</code>. But in the 2nd case where the user picks an image form the ...
  • ——-一、概述NSString是Foundation框架支持的一个类,用于创建和处理字符串对象。在OC语言中,创建一个常量字符串对象时,开头加@符号。本文中主要介绍如何在OC中创建一个NSSting字符串对象,并输出。介绍NSString...
  • self.orderCost.text = [NSString stringWithFormat:@"%.1f元",self.order.cost.floatValue]; %.1f 表示小数点一位,%.2f 表示小数点2位,依次类推. 格式定义 The format specifiers supported by the ...
1 2 3 4 5 ... 20
收藏数 237,232
精华内容 94,892
关键字:

nsstring