oc -- Foundation framework -- string

Create string

NSString represents a string whose character sequence is immutable. NSString is very powerful. oc string processing is much simpler and easier to use than C language characters.
NSString has these functions:
πŸŽ‡ To create a string, you can use the instance method starting with init, the class method starting with string, or the form of @ ""
πŸŽ‡ Read the file or network URL to initialize the string
πŸŽ‡ Writes the string contents to a file or URL
πŸŽ‡ Get the length of the string. You can get the number of characters or bytes
πŸŽ‡ Gets the character or byte in the string, which can be in a specified position or in a specified range
πŸŽ‡ Get the C-style string corresponding to the string
πŸŽ‡ Connection string
πŸŽ‡ Delimited string
πŸŽ‡ Finds the specified character and string in a string
πŸŽ‡ Replace string
πŸŽ‡ Compare strings
πŸŽ‡ String comparison size
πŸŽ‡ Converts the case of characters in a string

Common functions of NSString

After getting the NSString string, you can call a large number of functional methods of NSString. For the usage of these methods, please refer to the reference manual of NSString.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString * str = @"123456789";
        NSRange pos = [str rangeOfString : @"4567"];
        NSLog(@"123 stay str Start position in:%ld,Length:%ld",pos.location,pos.length);
        str = [str stringByAppendingString :@"10"];
        //Use stringByAppendingString to add 10 after 123456789

Operation results:

In the above program, a variable of type NSRange is used. NSRange is not a class, but a structure. It includes two unsigned int integer values of location and length, representing the starting position and length respectively.

Variable string (NSMutableString)

The NSString class is immutable, that is, once the NSString object is created, the character sequence contained in the object cannot be changed until the object is destroyed.
NSMutableString object represents a string with variable character sequence, and NSMutableString is a subclass of NSString. Therefore, NSMutableString can be used directly for the methods contained in NSString, and NSMutableString object can also be used directly as NSString object.
After an NSMutableString is created, the character sequence contained in the string can be changed through the appendFormat:, appendString:, deleteCharacterInRange:, insertString: atIndex:, replaceCharactersInRange: withString:, replaceoccurrenceofstring: withString: options: range:, setString: methods provided by NSMutableString.
Some of these methods are used in the following procedure:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSString * book = @"<insane iOS Handout";
        //Create an NSMutableString object
        NSMutableString * str = [NSMutableString stringWithString:@"Hello"];
        //Append fixed string
        //The character sequence contained in the string itself has changed, so there is no need to re assign
        [str appendString:@",iOS!"];
        //Append string with variable
        //The character sequence contained in the string itself has changed, so there is no need to re assign
        [str appendFormat:@"%@It's a very good book",book];
        //Inserts a string at the specified location
        //The character sequence contained in the string itself has changed. Therefore, no re assignment is required
        [str insertString:@"fkit.org" atIndex:6];
        //Delete all characters from position 6 to position 12
        [str deleteCharactersInRange:NSMakeRange(6,12)];
        //Replace the string from position 6 to position 9 with Objective-c
        [str replaceCharactersInRange: NSMakeRange(6,9)withString:@"Objective-c"];

Program output:

Tags: iOS xcode objective-c

Posted by kh411dz on Wed, 18 May 2022 02:43:07 +0300