Some common methods of OC NSString

During this period of time, I was writing a project, and suddenly found that I had forgotten a lot of knowledge. Now that I have time, I will first write down some common methods of NSString in OC, which can be regarded as a recollection.

// custom initial method

NSString *str1 = [[NSString alloc]initWithFormat:@"Hello kitty"];

// use class method

NSString *str2 = [NSString stringWithFormat:@"Hello world"];

// use literal method

NSString *str3 = @"Hello";

The above three methods are more commonly used. In addition, in MRC, str1 requires us to manually release the memory, otherwise there will be memory leaks

The following methods about NSString are more commonly used:

// Get the length of the string: [str1 length];

// Get the character at the specified position, note: the position of the obtained character cannot exceed the length of the string

[str1 characterAtIndex:index]; // index is the position of the character you want to take

// String interception: intercept by position and length

NSString *subStr1 = [str1 substringWithRange:NSMa可Range(4, 4)];

// intercept the specified position backwards

NSString *subStr2 = [str1 substringFromIndex: 6];

// The specified position is intercepted forward, excluding the specified position

NSString *subStr3 = [str1 substringToIndex:4];

// concatenation of strings

NSString *str = [str1 stringByAppendingString:str2];

// string comparison

int a = [str1 compare:str2];

// Whether the strings are equal, this is very common in engineering, often used

BOOL isEaual = [str1 isEqualToString:str2];

// Convert between string case: all characters are uppercase

NSString *string1 = [str1 uppercaseString]; // This is also more commonly used. It is used in subsequent network requests to prevent hand mistakes and mixed use of upper and lower case. Using this can effectively prevent

// all characters are lowercase

NSString *string2 = [str1 lowercaseString];

// capitalize first letter

NSString *string3 = [str1 capitalizedString];

The above can only be some common methods that I have been exposed to more at this stage. If you use them in the process of using strings, and if you forget some methods, you can directly use comment + left-click to enter the class library of the detailed introduction of strings. Find the method you need.

If there are mistakes, please correct them



Guess you like

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