OC string

 unichar data[6] = {97,98,99,100,101,102};

        //Initialize the string with an array of Unicode values

        NSString* str = [[NSString alloc]initWithCharacters:data length:6];

        NSLog(@"%@",str);

        //Convert C-style strings to NSString objects

        char* cstr = "Hello,iOS!";

        NSString* str2 = [NSString stringWithUTF8String:cstr];

        NSLog(@"%@",str2);

        //write the string to the specified file

        [str2 writeToFile:@"myFile.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];

        //Read the file content, initialize the string with the file content

        NSString* str3 = [NSString stringWithContentsOfFile:@"myFile.txt" encoding:NSUTF8StringEncoding error:nil];

        NSLog(@"%@",str3);

        

        NSString* str4 = @"hello";

        NSString* str5 = @" word";

        //Append a fixed string after str

        str4 = [str4 stringByAppendingString:str5];

        NSLog(@"%@",str4);

        const char*conStr= [str4 UTF8String];

        NSLog ( @"Get the C-style string for the C string: %s" ,conStr);

        NSLog ( @"str is decoded according to UTF-8 character set: %lu" ,[str4 lengthOfBytesUsingEncoding :NSUTF8StringEncoding]);

        NSLog ( @"Get the string consisting of the first 10 characters of str: %@" ,[str4 substringToIndex: 10 ]);

        NSLog ( @"Get str starting from the 5th character, to the following string:%@" ,[str4 substringFromIndex: 5 ]);

        //[str substringWithRange:NSMakeRange(5,3)] starts from the 5th and intercepts 3 lengths

        NSLog ( @"Get the string from the 5th character to the 10th character:%@" ,[str4 substringWithRange:NSMakeRange( 5 , 3 )]);

        //Get the position of hello in the character

        NSRange range = [str4 rangeOfString:@"hello"];

        NSLog ( @"Get the position of hello in the character:%ld,length:%ld" ,range.location,range.length);

        //Convert str characters to uppercase

        str4 = [str4 uppercaseString];

        NSLog(@"%@",str4);


===============================================

 NSString* str = @" word";

        NSMutableString* MTStr = [NSMutableStringstringWithFormat:@"Hello"];

        // Append fixed string

        [MTStr appendString:str];

        NSLog(@"%@",MTStr);

        // Append string with variable

        [MTStr appendFormat:@"%@",@"呵呵呵"];

         NSLog(@"%@",MTStr);

        // Append the string at the specified position

        [MTStr insertString:@"OOO" atIndex:5];

         NSLog(@"%@",MTStr);

        // delete all strings from position 5 to position 8

        [MTStr deleteCharactersInRange:NSMakeRange(5, 3)];

         NSLog(@"%@",MTStr);

        //Replace "hehehe" with "!!!"

        [MTStr replaceCharactersInRange:NSMakeRange(10, 3) withString:@"!!!"];

         NSLog(@"%@",MTStr);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326131880&siteId=291194637