Objective C string (3)

Source: Internet
Author: User

Original holydancer. If you need to reprint it, please indicate it in the explicit position:

Csdn column converted from holydancer, original address: http://blog.csdn.net/holydancer/article/details/7343561

 

String operations in Objective C

 

When creating a string in OC, C is generally not used, because C uses the string as a character array, so there will be a lot of inconvenience during the operation, some methods of nsstring integration in cocoa can easily operate strings. The following are several examples:

 

1. Create:

Assign values directly using equal signs

Nsstring * [email protected] "I Am a string ";

Stringwithformat Method

Nsstring * str2 = [nsstring stringwithformat: @ "I Am a parameter string % d, % d", 11,111 "];

2. Merge:

Nsstring is unchangeable. A new string can be generated based on the original string, but the original string is not changed. However, nsstring provides a subclass: nsmutablestring. this class is variable. The available nsstring methods also apply to nsmutablestring, but you must declare it before using it:

Nsmutablestring * str3 = [nsmutablestring stringwithcapacity: 50];

It should be noted that although 50 capacity is provided during the declaration, nsmutablestring will be automatically expanded, so there is no need to worry about the small space.

After the nsmutablestring is created, you can merge the nsmutablestring. Common methods include:

 

[Plain]View plaincopy
  1. Nsmutablestring * str3 = [nsmutablestring stringwithcapacity: 50];
  2. Nsstring * [email protected] "nsstring string ";
  3. [Str3 appendstring: str4];
  4. [Str3 appendformat: @ "% d", 5];
  5. Nslog (str3 );

The output result of the above Code is:"NsstringString5 ";

 

3. Delete:

String Deletion,A secondary struct: nsange;

The length and location attributes of the nsange can be used for graphic assistance and string assistance. In string assistance, it indicates a range of strings. Location indicates the starting position, length indicates the number of characters. There are three methods to create the nsange:

(1)

Nsange range;

Range. Location = 10;

Range. Length = 2;

(2)

Nsange range = {10, 2 };

(3)

Nsange range = nsmakerange (); // This method is commonly used.

The following code shows how to use the nsange tool to modify the string content:

 

 

[Plain]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2. Int main (INT argc, const char * argv [])
  3. {
  4. @ Autoreleasepool {
  5. Nsmutablestring * STR = [nsmutablestring stringwithcapacity: 5];
  6. [STR appendstring: @ "I am holydancer"];
  7. // Determine if "am" exists in STR and retrieve its range.
  8. Nsange range = [STR rangeofstring: @ "am"];
  9. [STR deletecharactersinrange: range];
  10. Nslog (@ "% @, % lu, % lu", STR, range. length, range. Location );
  11. }
  12. Return 0;
  13. }

The output result is as follows:

I holydancer, 2, 2

 

4. comparison:

(1) Comparison of equal values:

Similar to some situations in Java, strings cannot be directly compared with =, =, which compares whether two strings are the same object. What we need to compare is whether the two strings are equal, in this case, we should use isequaltostring instead of = to compare its pointer value. The return value of isequaltostring is of the bool type, which is yes and no.

 

[Plain]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2. Int main (INT argc, const char * argv [])
  3. {
  4. @ Autoreleasepool {
  5. Nsstring * str1 = @ "holydancer ";
  6. Nsstring * str2 = @ "holydancer ";
  7. If ([str1 is1_tostring: str2])
  8. {
  9. Nslog (@ "Equal string content ");
  10. }
  11. }
  12. Return 0;
  13. }

(2) Comparison of string size:

 

The size of a string varies according to the order of arrangement. If we want to compare the size of two strings, we can use the compare method. The compare method returns three types of results: nsorderedascending, nsorderedsame, and nsordereddescending. In addition, when using the compare method, you can add parameters to determine whether the results are case sensitive, or, it is declared as comparing the number of strings, not character values.

See the following demo for specific usage:

 

[Plain]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2. Int main (INT argc, const char * argv [])
  3. {
  4. @ Autoreleasepool {
  5. Nsstring * str1 = @ "holydancer ";
  6. Nsstring * str2 = @ "dancerholy ";
  7. Nscomparisonresult result1 = [str1 compare: str2];
  8. If (result1 = nsorderedsame ){
  9. Nslog (@ "equal ");
  10. } Else if (result1 = nsorderedascending ){
  11. Nslog (@ "str1 less than str2 ");
  12. } Else {
  13. Nslog (@ "str1 greater than str2 ");
  14. }
  15. }
  16. Return 0;
  17. }

 

To ignore case sensitivity, you can add the nscaseinsensitivesearch parameter in options.

Nscomparisonresult result2 = [str1 compare: str2 options: nscaseinsensitivesearch];

The conditions corresponding to nscaseinsensitivesearch include nsliteralsearch (case-sensitive) and nsnumericsearch (compared by the number of characters). You can use "|" to satisfy multiple conditions at the same time.

(3) determine the start and end of a string

The hasprefix method determines whether to start with a string and hassuffix determines whether to end with a string.

 

[Plain]View plaincopy
  1. # Import <Foundation/Foundation. h>
  2. Int main (INT argc, const char * argv [])
  3. {
  4. @ Autoreleasepool {
  5. Nsstring * str1 = @ "holydancer ";
  6. If ([str1 hasprefix: @ "holy"]) {
  7. Nslog (@ "str1 starts with holy ");
  8. }
  9. If ([str1 hassuffix: @ "dancer"]) {
  10. Nslog (@ "str1 ends with Dancer ");
  11. }
  12. }
  13. Return 0;
  14. }

[4] judge that the string is null and has no value (the length is 0)

Nsstring * urlstring = "";

If (! Urlstring & [urlstring length] = 0)

{

Nslog (@ "the string is null or the length is 0 ");

}

}

 

Keywords: Objective-C, Objective C, OC, string

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.