[Objective-C] basic concepts and common handling methods of strings (NSString) in OC

Source: Internet
Author: User

In the Foundation framework, the NNString class is provided to process string objects. In C, strings are represented in a pair of quotation marks.

OC is an extension of C language. In other words, the OC language is built on the basis of C language (based on C, it adds object-oriented content, smalltalk language ).

Therefore, to distinguish between the two strings, the OC-language string must start with @, And the quotation marks must be the content of the string, such as @ "abc". In addition, the NSString object,

Once created, it cannot be modified. If you need a variable String object, you need to create an NSMutableString instance.

1: The following are some methods for creating strings:

 

// Create a String constant

NSString * string1 = @ "This string constant"; // create an empty string NSString * string2 = [[NSString alloc] init]; NSString * string3 = [NSString string]; // quickly create a string NSString * string4 = [[NSString alloc] initWithString: @ "quickly create a string"]; NSString * string5 = [NSString stringWithString: @ "quickly create a string"]; // quickly create a formatted string int number = 2; NSString * string6 = [[NSString alloc] initWithFormat: @ "% d ", number]; NSString * string7 = [NSString stringWithFormat: @ "% d", number];

 

2: compare whether the content of the string is the same

 

 

NSString * string6 = [[NSString alloc] initWithFormat: @ "% d", number]; NSString * string7 = [NSString stringWithFormat: @ "% d", number]; NSLog (@ "% @, % @", string1, string2, string3, string4, string5, string6, string7); // compare the two strings if ([string6 isw.tostring: string7]) {NSLog (@ "same content ");} else {NSLog (@ "different content ");}

 

3: compare whether two strings are the same object

 

 

// Compare whether two strings are the same object NSString * string8 = [NSString stringWithString: @ "123"]; NSString * string9 = [NSString stringWithString: @ "123"]; if ([string8 isw.tostring: string9]) {NSLog (@ "same content");} else {NSLog (@ "different content");} if (string8 = string9) {NSLog (@ "same object");} else {NSLog (@ "different objects ");}

 

4: Compare the string size

 

 

// Compare the string size NSString * string10 = [[NSString alloc] initWithString: @ "a"]; NSString * string11 = [[NSString alloc] initWithString: @ "B"]; NSComparisonResult result = [string10 caseInsensitiveCompare: string11]; NSLog (@ "% ld", result); //-1 indicates ascending

 

 

5: there are also some common methods to create strings:

 

 

- (id)init;- (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */- (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length;- (id)initWithUTF8String:(const char *)nullTerminatedCString;- (id)initWithString:(NSString *)aString;- (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);- (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);- (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;- (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */+ (id)string;+ (id)stringWithString:(NSString *)string;+ (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length;+ (id)stringWithUTF8String:(const char *)nullTerminatedCString;+ (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);+ (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;/* These use the specified encoding.  If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).*/- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;/* These try to determine the encoding, and return the encoding which was used.  Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).*/- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;
6: Evaluate the length of the string

 

 

 

// Evaluate the length of the string NSString * string12 = [[NSString alloc] initWithString: @ "abcd"]; NSLog (@ "string2 length: % ld ", [string12 length]);

 

 

7: String Conversion

 

// String Conversion

 

NSString *string13=[[NSString alloc]initWithString:@"HelloWorld"];        NSLog(@"upper %@",[string13 uppercaseString]);        NSLog(@"lower %@",[string13 lowercaseString]);        NSLog(@"capitalized %@",[string13 capitalizedString]);
8: convert a string to a basic data type
 
// Convert the string to the basic data type NSString * string14 = [[NSString alloc] initWithString: @ "3.14"]; NSLog (@ "%. 2f \ n ", [string14 floatValue]);
9: convert a string to an array
  
// Convert the string to an array NSString * string15 = [[NSString alloc] initWithString: @ "abc bcd xyz"]; NSArray * array = [string15 componentsSeparatedByString: @ ""]; NSLog (@ "% @ \ n", array );
10: String Truncation
 
 
// Capture the string NSString * string16 = @ "abcdefg"; NSLog (@ "% @", [string16 substringToIndex: 2]); NSLog (@ "% @", [string16 substringFromIndex: 2]); nsange range; range. location = 2; range. length = 3; NSLog (@ "% @", [string16 substringWithRange: range]);
11: String concatenation:
   
// String concatenation NSString * string17 = @ "abc"; NSString * string18 = @ "xyz"; NSString * appString = [[NSString alloc] initWithFormat: @ "this is the concatenated string: % @ and % @", string17, string18]; NSString * appString2 = [string17 stringByAppendingString: @ "123"]; NSString * appstring3 = [string17 stringByAppendingFormat: @ "% @", string18]; NSLog (@ "% @, % @, % @", appString, appString2, appstring3 );
12: string SEARCH:
  
// String query NSString * link = @ "abcdffe-= fefjfwfw"; nsange range1 = [link rangeOfString: @ "abcd"]; NSLog (@ "% @", NSStringFromRange (range1); if (range1.location! = NSNotFound) {NSLog (@ "found! ");}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.