Explanation of NSString in Objective-C

NSString is a basic class encapsulated in the Fundationg framework in Object-C that specializes in processing string data. It is a very important and frequently used basic class.

How to create NSString:

    NSString *firstString = @"Justin";  //第一种方式
    NSString *secondString = [NSString new]; //第二种方式
    NSString *thirdString = [NSString string];  //第三种方式
    NSString *forthString = [[NSString alloc] init];//第四种方式

NSString constancy:

The string object created in the first method above will be stored in the data area in the memory . The variables and constants stored in this area will only be released by the memory after the program ends. The string objects created in the second to fourth methods above will be created in the memory heap.

No matter how it is created, modify it again:

    NSString *firstString = @"Justin";  
    firstString = @"凡人皆有一死,凡人皆需侍奉。"

The original string @"Justin" memory space will not be replaced with a new string value, but a corresponding string @"Everyone is mortal, and all mortals need to serve." A new corresponding string is created in the memory space to store the new string value . String, let firstString point to the newly created memory address.

Principle of the system operating mechanism: Whenever an NSString object is created, it will search for a string with the same content in the current memory area. If the same string exists in the memory area, no new memory space will be created. is to point the pointer to the memory address of the same string. If the system does not find the same string content, it will create a new one. In addition, if a string is newly created in the data area, the system will not check whether there is the same string in the heap, and the data area and heap area will not search across areas.

Comparison of NSString objects:

    NSString *firstString = @"Justin";
    NSString *secondString = @"Justin";
    NSString *thirdString = [NSString stringWithFormat:@"Justin"];
    
    if (firstString == secondString) {
        NSLog(@"YES");
    }
    
    if (firstString != thirdString) {
        NSLog(@"NO");
    }

The above code shows that there is a problem in just using pointers to determine whether two strings are equal, because as mentioned above, different NSString object creation methods will have two memory spaces: the data area and the heap. Therefore, once the two spaces are involved, To compare the contents of NSString objects, just using "==" is risky. The following should be used:

     if([firstString isEqualToString:thirdString]){
         NSLog(@"YES");
     }

Splicing method of NSString object:

    NSString *firstString = @"Justin";
    NSString *secondString = @"Justin";
    NSString *thirdString = [NSString stringWithFormat:@"%@%@",firstString,secondString];

How to get the length of NSString object:

    NSString *firstString = @"Justin";
    NSUInteger length = firstString.length;

Conversion of NSString object and C language Char type:

    //NSString->char

    NSString *firstString = @"Justin";
    char * myChar = firstString.UTF8String;

    //chart->NSString

    NSString *secondString = [NSString stringWithUTF8String:myChar];

NSString reads network address content

    //Get text from URL
    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
    NSString *str1 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding         error:nil];

NSString writes to local file

    NSString *text = @"Justin!";
    NSString *locoalPath = @"/Users/Justin/Desktop/abc.txt";
    NSError *error;
    BOOL resoult= [text writeToFile:locoalPath atomically:NO encoding:NSUTF8StringEncoding error:&error];
    
    if (error){
        NSLog(@"%@",error.localizedFailureReason);
    }else{
        NSLog(@"%d",resoult);
    }

NSString reads local file

    NSString *locoalPath = @"/Users/Justin/Desktop/abc.txt";
    NSError *error;
    
    NSString *text =[NSString stringWithContentsOfFile:locoalPath encoding:NSUTF8StringEncoding error:&error];

NSString object comparison (size, length...)

The return type is an enumeration type: you can check it specifically when using it.

options: is also an enumeration type. It specifies the conditions under which two strings are compared. It can be defined according to requirements when used.

    NSString *firstString = @"Justin";
    NSString *thirdString = @"swear you can";

    //NSComparisonResult:ENUM type
    //NSNumericSearch:ENUM type
    NSComparisonResult result = [firstString compare:thirdString options:NSNumericSearch];

NSString determines whether it starts or ends with a string:

    NSString *thirdString = @"swear you can";
    //判断是否以swear开头
    BOOL start = [firstString hasPrefix:@"swear"];
    //判断是否以can结尾
    BOOL end = [firstString hasPrefix:@"can"];

NSString determines whether it contains a certain string and the length of a certain string:

    NSString *thirdString = @"swear you can";
    NSRange rang = [thirdString rangeOfString:@"you"];
    NSLog(@"length=%lu,location=%lu",(unsigned long)rang.length,(unsigned long)rang.location);

NSNotFound is returned when not found.

NSString converts letters to uppercase:

string = [string uppercaseString];

NSString has many other usage methods and functions. During use, everyone can find more functions provided by the system based on experience to meet different needs.

Guess you like

Origin blog.csdn.net/JustinZYP/article/details/124187298