Nsstring of Objective-C

Time:2021-7-4

Create string

NSString *aStr = @"I am a String";
NSString *cStr = @"I am a String";

Creating strings with standard C

Char * CString = I am a string;  
NSString *astring = [[NSString alloc] initWithUTF8String:Cstring];  
NSLog(@"astring:%@",astring);

The memory address of the string

NSLog(@"astr point adress: 0x%.8x",aStr);
NSLog(@"astr point adress: 0x%.8x",bStr);
bStr = @"I am a String";
NSLog(@"%@",bStr);
NSLog(@"astr point adress: 0x%.8x",aStr);
NSLog(@"astr point adress: 0x%.8x",bStr);

2016-10-09 18:00:40.802217 NSStringDemo[13717:1142243] astr point adress: 0x00002060
2016-10-09 18:00:40.802360 NSStringDemo[13717:1142243] astr point adress: 0xb7b48920
2016-10-09 18:00:40.802499 NSStringDemo[13717:1142243] I am a String
2016-10-09 18:00:40.802512 NSStringDemo[13717:1142243] astr point adress: 0x00002060
2016-10-09 18:00:40.802521 NSStringDemo[13717:1142243] astr point adress: 0x00002060

format string

//Format output symbol
%@Objects  
%d. % I integer  
%U-infinity shaping  
%F floating point / double word  
%x. % x binary integer  
%O octal integer  
%zu    size_t  
%P pointer  
%E floating point / doubleword (Scientific Computing)  
%G floating point / double word  
%S C string  
%. * s Pascal string  
%C character  
%C     unichar  
%LLD 64 bit long integer  
%LLU unmatched 64 bit long integer  
%LF 64 bit doubleword  
%E is a real number, calculated by scientific counting

string comparison

void compare() {
    bool result = [aStr isEqualToString:cStr];
    if (result) {
        Nslog (@ "two strings are the same");
    } else {
        Nslog (@ "two strings are different");

    }
}

2016-10-09 18:00:12.548501 nsstringdemo [13701:1141412] two strings are the same

String segmentation

void split() {
    Nsstring * STR = @ "U.S., China, Japan, Russia, U.K.";
    //String array pointer
    NSArray *array = [str componentsSeparatedByString:@","];
    for (int i = 0; i < [array count]; i++) {
        NSLog(@"string: %@",[array objectAtIndex:i]);
    }
}

2016-10-09 18:00:40.802763 nsstringdemo [13717:1142243] string: USA
2016-10-09 18:00:40.802796 nsstringdemo [13717:1142243] string: China
2016-10-09 18:00:40.802806 nsstringdemo [13717:1142243] string: Japan
2016-10-09 18:00:40.802815 nsstringdemo [13717:1142243] string: Russia
2016-10-09 18:00:40.802823 nsstringdemo [13717:1142243] string: UK

string matching

void match() {
    Nsstring * STR = @ "U.S., China, Japan, Russia, U.K.";

    //Prefix
    If ([str hasprefix: @ "us"]){
        Nslog (@ "string starts with us");
    }

    //Suffix
    If ([str hassuffix: @ "UK"]){
        Nslog (@ "string ends in UK");
    }
}

2016-10-09 18:00:40.802835 nsstringdemo [13717:1142243] string starts with the United States
2016-10-09 18:00:40.802862 nsstringdemo [13717:1142243] string ends in UK

String search and replacement

void searchAndReplace() {
    Nsstring * string = @ "we are the successors of Gong industrialism";
    Nsstring * temp = @ "Gong";
    //String position
    NSRange range1 = [string rangeOfString:temp];
    Nslog (@ "starting position of temp in string: Lu.", (unsigned long) range1. Location));
    Nslog (@ "end position of temp in string: Lu.", (unsigned long) (range1. Location + range1. Length));

    //Replace string
    NSString *str = [string stringByReplacingCharactersInR ange:range1 withString : @ "communist"];
    NSLog(@"%@",str);

    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"%@",str);

}

2016-10-09 18:00:40.802877 nsstringdemo [13717:1142243] starting position of temp in string: 4
2016-10-09 18:00:40.802888 nsstringdemo [13717:1142243] end position of temp in string: 9
2016-10-09 18:00:40.802959 nsstringdemo [13717:1142243] we are the successors of communism
2016-10-09 18:00:40.803017 nsstringdemo [13717:1142243] we are the successors of communism
2016-10-09 18:00:40.803073 nstringdemo [13717:1142243] name: Liu Guoquan, age: 27
2016-10-09 18:00:40.803108 nsstringdemo [13717:1142243] name: liuguoquan, age: 27
2016-10-09 18:00:40.803126 nsstringdemo [13717:1142243] name: liuguoquan, age: 29