Several common NSSrting usage and nssrting usage

Source: Internet
Author: User
Tags float double truncated

Several common NSSrting usage and nssrting usage

1. The biggest difference between NSString and char * is that NSString is an objective object, and char * is a byte array. @ + "String" indicates the standard usage of the objective-c NSString String constant. @-(void) viewDidLoad [super viewDidLoad] is not required when char * is created. // classic string value NSString * str0 = @ "my name is justcoding! "; // String formatting and merging include // NSString * type int type char * type NSString * str1 = [NSString stringWithFormat: @" my name: % @ My age: % d my mailbox: % s ", @" justcoding ", 25," justcoding@gmail.com "]; // only one string can be written in the string assignment parameter, which is similar to NSString * str2 = [NSString stringWithString: @ "I Am a string"]; // The string to UTF-8 format parameter is char * type NSString * str3 = [NSString stringwithuf8string: "string to UTF-8 format"]; // string to merge int I = 100; char * c = "xuanyusong"; NSString * temp = @ "I am near Time string "; // Add int I and char * c to form a new string NSString * str4 = [temp stringByAppendingFormat: @" INTEGER: % d character type: % s ", I, c]; // Add temp Based on the string temp and form a new string NSString * str5 = [temp stringByAppendingString: temp]; // string output NSLog (@ "str0 = % @", str0); NSLog (@ "str1 = % @", str1); NSLog (@ "str2 = % @", str2); NSLog (@ "str3 = % @", str3); NSLog (@ "str4 = % @", str4); NSLog (@ "str5 = % @", str5);-(void) viewDidLoad {[Super viewDidLoad]; // classic string value NSString * str0 = @ "my name is justcoding! "; // String formatting and merging include // NSString * type int type char * type NSString * str1 = [NSString stringWithFormat: @" my name: % @ My age: % d my mailbox: % s ", @" justcoding ", 25," justcoding@gmail.com "]; // only one string can be written in the string assignment parameter, which is similar to NSString * str2 = [NSString stringWithString: @ "I Am a string"]; // The string to UTF-8 format parameter is char * type NSString * str3 = [NSString stringwithuf8string: "string to UTF-8 format"]; // string to merge int I = 100; char * c = "xuanyusong"; NSString * temp = @ "I am near Time string "; // Add int I and char * c to form a new string NSString * str4 = [temp stringByAppendingFormat: @" INTEGER: % d character type: % s ", I, c]; // Add temp Based on the string temp and form a new string NSString * str5 = [temp stringByAppendingString: temp]; // string output NSLog (@ "str0 = % @", str0); NSLog (@ "str1 = % @", str1); NSLog (@ "str2 = % @", str2); NSLog (@ "str3 = % @", str3); NSLog (@ "str4 = % @", str4); NSLog (@ "str5 = % @", str5);} 2. the traversal of each string is actually caused It consists of dry char characters. The traversal of a string is to extract each character from the string. -(Void) viewDidLoad [super viewDidLoad]; // classic string value assignment NSString * str = @ "YUSONGMOMO"; // String length int count = [str length]; NSLog (@ "the string length is % d", count); // traverse each character in the string for (int I = 0; I <count; I ++) char c = [str characterAtIndex: I]; NSLog (@ "The % d character of the string is % c", I, c);-(void) viewDidLoad {[super viewDidLoad]; // The classic string value NSString * str = @ "YUSONGMOMO"; // the string length int count = [str length]; NSLog (@ "the string length is % d ", count );/ /Traverse each character in the string for (int I = 0; I <count; I ++) {char c = [str characterAtIndex: I]; NSLog (@ "string % d bit: % c", I, c) ;}} 3. the Comparison Between strings is is‑tostring to determine whether the strings are completely equal, and the case cannot be completely matched. HasPrefixe matches the string header haSuffix matches the string's tail-(void) viewDidLoad [super viewDidLoad]; NSString * str0 = @ "justcoding"; NSString * str1 = @ "justcoding "; // if ([str0 isEqualToString: str1]) NSLog (@ "string is completely equal"); // if ([str0 hasPrefix: @ "just"]) NSLog (@ "string str0 starts with just"); // string comparison with if ([str1 hasSuffix: @ "coding"]) NSLog (@ "str1 string ending with coding");-(void) viewDidLoad {[super viewDidLoad]; NSString * str0 = @ "ju Stcoding "; NSString * str1 = @" justcoding "; // if ([str0 is1_tostring: str1]) {NSLog (@" string completely equal ");} // compare if ([str0 hasPrefix: @ "just"]) {NSLog (@ "string str0 starts with just ");} // The string ends with if ([str1 hasSuffix: @ "coding"]) {NSLog (@ "str1 string ends with coding ");}} // isEqualToString method NSString * astring01 = @ "This is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 ishangtostring: astring02]; NSLog (@" result: % d ", result); // ishangtostring method NSString * astring01 = @" This is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 isw.tostring: astring02]; NSLog (@" result: % d ", result); // compare method (three values returned by comparer) NSString * astring01 = @ "This is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedSame; NSLog (@" result: % d ", result ); // NSOrderedSame determines whether the content of the two is the same // The compare method (three values returned by comparer) NSString * astring01 = @ "This is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedSame; NSLog (@" result: % d ", result ); // NSOrderedSame determines whether the content is the same NSString * astring01 = @ "This is a String! "; NSString * astring02 = @" this is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedAscending; NSLog (@" result: % d ", result ); // NSOrderedAscending determines the size of the two object values (in alphabetical order, astring02 is greater than astring01 is true) NSString * astring01 = @ "This is a String! "; NSString * astring02 = @" this is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedAscending; NSLog (@" result: % d ", result ); // NSOrderedAscending determines the size of the two object values (in alphabetical order, astring02 is greater than astring01 is true) NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedDescending; NSLog (@" result: % d ", result ); // NSOrderedDescending determines the size of the two object values (in alphabetical order, astring02 is less than astring01 is true) NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02] = NSOrderedDescending; NSLog (@" result: % d ", result ); // NSOrderedDescending determines the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01) // The comparison String 1 NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame; NSLog (@" result: % d ", result ); // NSOrderedDescending is used to determine the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01) // The comparison String 1 NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame; NSLog (@" result: % d ", result ); // NSOrderedDescending is used to determine the size of the two object values (which are compared alphabetically. astring02 is true if it is smaller than astring01) // compare String 2 NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02 options: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame; NSLog (@" result: % d ", result); // NSCaseInsensitiveSearch: case-insensitive comparison NSLiteralSearch: performs a full comparison. case-sensitive NSNumericSearch: compares the number of characters in a string, rather than the character value. // Compare String 2 NSString * astring01 = @ "this is a String! "; NSString * astring02 = @" This is a String! "; BOOL result = [astring01 compare: astring02 options: NSCaseInsensitiveSearch | NSNumericSearch] = NSOrderedSame; NSLog (@" result: % d ", result); // NSCaseInsensitiveSearch: case-insensitive comparison NSLiteralSearch: performs a full comparison. case-sensitive NSNumericSearch: compares the number of characters in a string, rather than the character value. The method of determination can use caseInsensitiveCompare in the NSString category to determine the order relationship between two strings by returning the values of-1, 0, or 1. The program is as follows. NSString * string = @ "0"; NSComparisonResult result = [string caseInsensitiveCompare: @ "A"]; switch (result) {case NSOrderedAscending: NSLog (@ "power-up"); break; case NSOrderedSame: NSLog (@ "ignore strings with the same case"); break; case NSOrderedDescending: NSLog (@ "power down"); break; default: NSLog (@ "unidentifiable"); break; NSString * string = @ "0"; NSComparisonResult result = [string caseInsensitiveCompare: @ "A"]; switch (result) {case NSOr DeredAscending: NSLog (@ "power-up"); break; case NSOrderedSame: NSLog (@ "ignore strings of the same case"); break; case NSOrderedDescending: NSLog (@ "power-down "); break; default: NSLog (@ "cannot be determined"); break;} 4. file string operation (read/write) // read string from the file: initWithContentsOfFile method NSString * path = @ "astring. text "; NSString * astring = [[NSString alloc] initWithContentsOfFile: path]; NSLog (@" astring: % @ ", astring); [astring release]; // astring = nil; // read the string from the file: initWi ThContentsOfFile method NSString * path = @ "astring. text "; NSString * astring = [[NSString alloc] initWithContentsOfFile: path]; NSLog (@" astring: % @ ", astring); [astring release]; // astring = nil; // write the String to the file: writeToFile method NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "]; NSLog (@" astring: % @ ", astring); NSString * path = @" astring. text "; [astring writeToFile: path atomically: YES]; [astring release]; // astring = nil; // write a string to the file: writeToFile method NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "]; NSLog (@" astring: % @ ", astring); NSString * path = @" astring. text "; [astring writeToFile: path atomically: YES]; [astring release]; // astring = nil; * ios5 does not support release iOS Application Development: What is ARC? // Extension Path NSString * Path = @"~ /NSData.txt "; NSString * absolutePath = [Path stringByExpandingTildeInPath]; NSLog (@" absolutePath: % @ ", absolutePath); NSLog (@" Path: % @", [absolutePath stringByAbbreviatingWithTildeInPath]); // extended Path NSString * Path = @"~ /NSData.txt "; NSString * absolutePath = [Path stringByExpandingTildeInPath]; NSLog (@" absolutePath: % @ ", absolutePath); NSLog (@" Path: % @", [absolutePath stringByAbbreviatingWithTildeInPath]); // file extension NSString * Path = @"~ /NSData.txt "; NSLog (@" Extension: % @ ", [Path pathExtension]); // file Extension NSString * Path = @"~ /NSData.txt "; NSLog (@" Extension: % @ ", [Path pathExtension]); 5. string truncation and Case sensitivity // NSString * string1 = @ "A String"; NSString * string2 = @ "String"; NSLog (@ "string1: % @", [string1 uppercaseString]); // upper-case NSLog (@ "string2: % @", [string2 lowercaseString]); // lower-case NSLog (@ "string2: % @", [string2 capitalizedString]); // the size of the first letter // NSString * string1 = @ "A String"; NSString * string2 = @ "String "; NSLog (@ "string1: % @", [String1 uppercaseString]); // upper-case NSLog (@ "string2: % @", [string2 lowercaseString]); // lower-case NSLog (@ "string2: % @", [string2 capitalizedString]); // Java code with the initial letter size-(void) viewDidLoad [super viewDidLoad]; NSString * str0 = @ "Chinese my name is xuanyusong "; // content NSString * to = [str0 substringToIndex: 4]; NSLog (@ "to = % @", ); // The content of the truncated Character index between 2 and the end NSString * from = [str0 substringFromIndex: 2]; NSLog (@ "f Rom = % @ ", from); // you can specify the range of the string to be truncated. // starting from the second place, the length is 10. nsange rang = NSMakeRange (2, 10 ); NSString * strRang = [str0 substringWithRange: rang]; NSLog (@ "rang = % @", strRang); // set the upper-case NSLog (@ "str0 upper-case: % @ ", [str0 capitalizedString]); // you can specify uppercase NSLog for all strings (@" str0 uppercase: % @ ", [str0 uppercaseString]). // set all characters to lowercase NSLog (@ "str0 lower case: % @", [str0 lowercaseString]);-(void) viewDidLoad {[super viewDidLoad]; NSString * str0 = @ "Chinese my name is xuanyusong"; // content NSString * to = [str0 substringToIndex: 4] from the starting point of the string to 4; NSLog (@ "to = % @", to); // The content NSString * from = [str0 substringFromIndex: 2] that intercepts the character index from 2 to the end; NSLog (@ "from = % @", from); // you can specify the range of the string to be truncated. // The length starts from the second digit and ranges from 10 to NSMakeRange rang = NSMakeRange (2, 10 ); NSString * strRang = [str0 substringWithRange: rang]; NSLog (@ "rang = % @", strRang); // set the upper-case NSLog (@ "str0 upper-case: % @ ", [str0 cap ItalizedString]); // set all strings to uppercase NSLog (@ "str0 capital: % @", [str0 uppercaseString]); // set all characters to lowercase NSLog (@ "str0 lower case: % @", [str0 lowercaseString]);} 6. search string and replace string-(void) viewDidLoad [super viewDidLoad]; NSString * str0 = @ "Chinese my name is xuanyusong"; NSString * temp = @ "is "; nsange rang = [str0 rangeOfString: temp]; NSLog (@ "index of the start point of the searched string in str0 is % d", rang. location); NSLog (@ "the index of the string to be searched in str0 is % d", rang. loc Ation + rang. length); // Replace the string in the search with a new string NSString * str = [str0 stringByReplacingCharactersInRange: rang withString: @ ""]; NSLog (@ "Replace the string with % @", str); // Replace "" in the string with * str = [str0 stringByReplacingOccurrencesOfString: @ "" withString: @ "@"]; NSLog (@ "after replacement, the string is % @", str);-(void) viewDidLoad {[super viewDidLoad]; NSString * str0 = @ "Chinese my name is xuanyusong"; NSString * temp = @ "is"; nsange rang = [Str0 rangeOfString: temp]; NSLog (@ "index of the string to be searched in str0 is % d", rang. location); NSLog (@ "the index of the string to be searched in str0 is % d", rang. location + rang. length); // Replace the string in the search with a new string NSString * str = [str0 stringByReplacingCharactersInRange: rang withString: @ ""]; NSLog (@ "Replace the string with % @", str); // Replace "" in the string with * str = [str0 stringByReplacingOccurrencesOfString: @ "" withString: @ "@"]; NSLog (@ "Replace the string with % @", str);} Extension Note: You can use the following method to replace the entire string and set the region to be replaced. StringByReplacingOccurrencesOfString :( NSString *) withString :( NSString *) options :( NSStringCompareOptions) range :( nsange) 7. you can use alloc to create a String object in the memory after adding a string to the end. You can dynamically operate on this string, modify and add it. Appendstring method: adds a string to the end of the string. AppendFormat: adds multiple types of strings to the end of a string. You can add any number or type of strings. -(Void) viewDidLoad [super viewDidLoad]; NSMutableString * str = [[NSMutableString alloc] init]; // Add a common string [str appendString: @ "aaa"]; // Add the string Integer type [str appendFormat: @ "my name: % @ My age: % d my email: % s", @ "justcoding", 25, "justcoding@gmail.com"]; NSLog (@ "str = % @", str);-(void) viewDidLoad {[super viewDidLoad]; NSMutableString * str = [[NSMutableString alloc] init]; // Add a common string [str appendString: @ "aaa"]; // Add Add string integer character type [str appendFormat: @ "my name: % @ My age: % d my email: % s", @ "justcoding", 25, "justcoding@gmail.com"]; NSLog (@ "str = % @", str);} 8. the stringWithString method is used to create a string to initialize and assign a value to rangeOfString. The method returns a range of NSMakeRange (0, 3) in the input string) the value Range is 0 to 3rd characters in the string. deleteCharactersInRange: The Range parameter used to delete the string is used to delete the string. -(Void) viewDidLoad [super viewDidLoad]; // create a string NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is best! "]; // Delete the [str deleteCharactersInRange: [str rangeOfString: @" justcoding "]; NSLog (@" str = % @", str);-(void) viewDidLoad {[super viewDidLoad]; // create a string NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is best! "]; // Delete the [str deleteCharactersInRange: [str rangeOfString: @" justcoding "]; NSLog (@" str = % @", str);} 9. string insertion stringWithString method: used to create a string to initialize the value assignment insertString method: The String object inserted by the first parameter, and the position inserted by the second parameter. -(Void) viewDidLoad [super viewDidLoad]; // create a string NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is"]; // Insert the string [str insertString: @ "best man" atIndex: 10] In the str 10th bits; NSLog (@ "str = % @", str);-(void) viewDidLoad {[super viewDidLoad]; // create the string NSMutableString * str = [NSMutableString stringWithString: @ "justcoding is"]; // Insert the string [str insertString: @ "best man" atIndex: 10]; NSLog (@" Str = % @ ", str);} 10. string Copy-(void) viewDidLoad [super viewDidLoad]; // create a string NSMutableString * str1 = [NSMutableString stringWithString: @ "string 1"]; NSMutableString * str2; // string value str2 = str1; [str2 appendString: @ "and string 2"]; NSLog (@ "str1 = % @", str1 ); NSLog (@ "str2 = % @", str2);-(void) viewDidLoad {[super viewDidLoad]; // create a string NSMutableString * str1 = [NSMutableString stringWithString: @ "string 1"]; NSMu TableString * str2; // string value str2 = str1; [str2 appendString: @ "and string 2"]; NSLog (@ "str1 = % @", str1 ); NSLog (@ "str2 = % @", str2);} Why does the str1 data change after str2 is added? This is the charm of the pointer, because we operate on the pointer, str2 = str1 means that the two pointers point to a piece of memory at the same time, then the memory content pointed to by str2 will change after str1. 11. If the conversion parameter of the string and the specified type is invalid, no exception will be thrown, like converting the integer type in Chinese. No error is reported, but the conversion result is 0. The default value is. -(Void) viewDidLoad [super viewDidLoad]; // convert a string to an integer NSString * str0 = @ "1121"; // NSString * str0 = @ "China "; // convert the string to an integer int I = [str0 intValue]; NSLog (@ "converted: % I", I ); // convert the string to interger NSString * str1 = @ "1985"; // NSString * str1 = @ "China"; // convert the string to interger NSInteger ii = [str1 integerValue]; NSLog (@ "converted: % I", ii); // convert the string to double NSString * str2 = @ "3.145926"; // NSString * str2 = @ "China "; // convert the string to double d = [str2 doubleValue]; NSLog (@ "after conversion: % f", d ); // convert the string to float NSString * str3 = @ "3.145926"; // NSString * str3 = @ "China"; // convert the string to float double f = [str3 floatValue]; NSLog (@ "converted: % f", f);-(void) viewDidLoad {[super viewDidLoad]; // string to integer NSString * str0 = @ "1121 "; // NSString * str0 = @ "China"; // convert the string to an integer int I = [str0 intValue]; NSLog (@ "after conversion: % I", I ); // convert the string to interger NSString * str1 = @ "1985"; // NSString * str1 = @ "China"; // convert the string to interger NSInteger ii = [str1 integerValue]; NSLog (@ "converted: % I", ii); // convert the string to double NSString * str2 = @ "3.145926"; // NSString * str2 = @ "China "; // convert the string to double d = [str2 doubleValue]; NSLog (@ "after conversion: % f", d ); // convert the string to float NSString * str3 = @ "3.145926"; // NSString * str3 = @ "China"; // convert the string to float double f = [str3 floatValue]; NSLog (@ "converted: % f", f );}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.