Objective-C [other common NSString usage], objectivecnsstring
-------------------------------------------
Other common NSString usage
// NSString length, get each character in the string, type conversion, and remove leading and trailing Spaces
# Import <Foundation/Foundation. h>
Void test1 ()
{
// ① Obtain the length of the string and call the length method using the string (this is an object method, and the return value is an unsigned long integer)
NSString * str1 = @ "12345abc12345 Wang zhongyao"; // a single character, regardless of numbers, English letters, and Chinese characters
// NSUInteger length1 = [str1 length];
NSUInteger length1 = str1.length; // here the get method of length is called, which works the same as the previous line.
NSLog (@ "% ld", length1 );
}
Void test2 ()
{
// ② Obtain each character in the string
NSString * str2 = @ "faffafs123131,. 11 ~~~!! 23! "; // Here, Chinese characters are not considered in strings, because we use c language to print things. % c, % s, and % @ are not good.
NSUInteger len = str2.length;
Unichar c;
For (int I = 0; I <len; I ++ ){
C = [str2 characterAtIndex: I];
Printf ("% c", c );
}
}
Void test3 ()
{
// ③ Conversion of strings and other data types
//★First, we can convert many other data types to string types using one method:
Int a = 12;
Float B = 3.14f;
NSString * str1 = [NSString stringWithFormat: @ "% d", a];
NSString * str2 = [NSString stringWithFormat: @ "% f", B];
NSLog (@ "str1 =%@, str2 =%@", str1, str2 );
//★Convert a string to another data type
// Convert string to int type
NSString * str3 = @ "23 ";
Int I = [str3 intValue];
NSLog (@ "% d", I );
// Convert the string to the float Type
NSString * str4 = @" 1.234 ";
Float f = [str4 floatValue];
NSLog (@ "% f", f );
// Convert string to int type
NSString * str5 = @" 2343.3320 ";
Double d = [str5 doubleValue];
NSLog (@ "% lf", d );
}
Void test4 ()
{
// ④ Conversion between OC string objects and C language strings
// Convert an OC String object to a C-language string (the OC string is an object, so converting it to a C-language string calls the object method)
NSString * str1 = @ "fasfas121 ";
Const char * s1 = [str1 UTF8String]; // note that the return value of the UTF8String method is of the const char type.
NSLog (@ "s1 = % s", s1 );
// Convert a C string to an OC String object (the C string is not an object, so the string object to be converted to an OC can only be called using the NSString class)
Char ch [] = "fafsfas213 ";
NSString * ocStr = [NSString stringwithuf8string: ch];
NSLog (@ "ocStr = % @", ocStr );
}
Void test5 ()
{
// ⑤ Remove XXXXX from the beginning and end of the string
// First, the method name stringByTrimmingCharactersInSet must be written in the following four methods. What is removed? You have to check the class method called by the NSCharacterSet class later.
// 1. Remove spaces at the beginning and end of the string
NSString * str1 = @ "itc ast ";
NSString * newStr1 = [str1 stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];
NSLog (@ "------- % @ -------", newStr1 );
// 2. Remove the first and last uppercase characters of the string (if there are extra spaces at the beginning and end, the uppercase letters after the spaces will not be removed)
// For example, NSString * str22 = @ "IOSitAcastIT"; at this time, the upper and lower case letters are not removed.
NSString * str2 = @ "IOSitAcastIT ";
NSString * newStr2 = [str2 stringByTrimmingCharactersInSet: [NSCharacterSet uppercaseLetterCharacterSet];
NSLog (@ "------- % @ -------", newStr2 );
// There are still many ways to try it by yourself. This part does not have to be memorized. It will be used when you use it.
}
Int main (int argc, const char * argv []) {
@ Autoreleasepool {
// Call test1 ~ during verification ~ Test5 function.
}
Return 0;
}
-------------------------------------------
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.