Objective-c----NSString, nsmutablestring

Source: Internet
Author: User


Directly on the code:
 
//
// main.m
//
// Created by on 15/4/2.
// Copyright (c) 2015. All rights reserved.
//

#import <Foundation / Foundation.h>
#import "Tire.h"


int main (int argc, const char * argv []) {

    // NSString class
    / *
     * Learning system classes need to grasp a few points:
     * 1. Create an object according to the given initialization method and convenience constructor method;
     * 2. Use the instance method to complete the corresponding operation according to the requirements;
     * 3. Learn to read system header files;
     * 4. Learn to learn the corresponding method through the help file.
     * /

    // Create using initialization method
// NSString * str1 = [[NSString alloc] initWithString: @ "name"];
    NSString * str1 = @ "name";
    NSLog (@ "% @", str1);
// NSString * str2 = [NSString stringWithString: @ "name"];
    NSString * str2 = @ "name";
    NSLog (@ "% @", str2);

    char * cStr = "haha";
    // Convert C language string to OC object
    NSString * str3 = [[NSString alloc] initWithCString: cStr encoding: NSUTF8StringEncoding];
    NSLog (@ "% @", str3);

    NSString * str4 = [NSString stringWithCString: cStr encoding: NSUTF8StringEncoding];
    NSLog (@ "% @", str4);

    // Create a string according to the specified format
    NSString * str5 = [[NSString alloc] initWithFormat: @ "% @ +% d", @ "en", 1001];
    NSLog (@ "% @", str5);

    NSString * str6 = [NSString stringWithFormat: @ "% @ +% d", @ "ne", 10014];
    NSLog (@ "% @", str6);

    // Create a string object based on the file contents of the specified path
    NSString * str7 = [[NSString alloc] initWithContentsOfFile: @ "/ Users / lanouhn / Desktop / test.txt" encoding: NSUTF8StringEncoding error: nil];
    NSLog (@ "% @", str7);

// NSError * err = [NSError init];
    NSString * str8 = [NSString stringWithContentsOfFile: @ "/ Users / lanouhn / Desktop / words.txt" encoding: NSUTF8StringEncoding error: nil];
    NSLog (@ "% @", str8);


    // Find the length of the string object
    NSInteger length = [str8 length];
    NSLog (@ "% ld", length);

    // Determine whether a string object has a prefix string
    BOOL result1 = [str8 hasPrefix: @ "If"];
    NSLog (@ "% @", result1? @ "YES": @ "NO");

    // Determine whether a string object has a suffix
    BOOL result2 = [str8 hasSuffix: @ "en"];
    NSLog (@ "% @", result2? @ "YES": @ "NO");

    // Determine if a string object is the same as another string object
    BOOL result3 = [str8 isEqualToString: str7];
    NSLog (@ "% @", result3? @ "YES": @ "NO");
    NSLog (@ "% d", result3);

    // String comparison and sorting results
    NSComparisonResult result4 = [str8 compare: str7];
    NSLog (@ "% ld", result4);

    // Get the substring
    // Substring starting from the specified subscript character (including modified characters) to the end of the string. The subscript starts from 0.
    NSString * subStr1 = [str8 substringFromIndex: 1];
    NSLog (@ "% @", subStr1);

    // From the beginning of the subscript 0 character to the end of the specified subscript, a string in this range
    NSString * subStr2 = [str8 substringToIndex: 2];
    NSLog (@ "% @", subStr2);

    // NSRange is a structure type, member location describes the index position, member length describes the length of the string to be intercepted
    NSRange rang = NSMakeRange (1, 3);
// rang.length = 4;
// rang.location = 2;
    NSString * subStr3 = [str8 substringWithRange: rang];
    NSLog (@ "% @", subStr3);

    // string stitching
    // Concatenate and generate a new string based on the given parameter string, without changing the original string. (Immutable string)
    NSString * newString1 = [str8 stringByAppendingString: @ "+ 100"];
    NSLog (@ "% @", newString1);

    // Generate a new string based on the given format string and parameter splicing
    NSString * newString2 = [str8 stringByAppendingFormat: @ "% d", 1001];
    NSLog (@ "% @", newString2);

    // Path stitching
    NSString * newString3 = [str8 stringByAppendingPathComponent: @ "xx.avi"];
    NSLog (@ "% @", newString3);

    // String replacement
    // Replace the strings that exist in str8 with the given second string, all are replaced
    NSString * newString4 = [str8 stringByReplacingOccurrencesOfString: @ "e" withString: @ "呵呵"];
    NSLog (@ "% @", newString4);

    // Conversion of string and numeric data
    NSString * numString1 = @ "1";
    NSInteger integerValue = [numString1 integerValue];
    NSLog (@ "% ld", integerValue);
    float integerValue1 = [numString1 floatValue];
    NSLog (@ "% f", integerValue1);

    // case conversion
    NSString * string = @ "i love you";
    // Convert to uppercase characters
    NSString * upperCaseStr = [string uppercaseString];
    NSLog (@ "% @", upperCaseStr);
    // Convert to lowercase characters
    NSString * lowercaseStr = [upperCaseStr lowercaseString];
    NSLog (@ "% @", lowercaseStr);

    // Convert to uppercase string (the first letter of each string is converted to uppercase)
    NSString * capitalString = [string capitalizedString];
    NSLog (@ "% @", capitalString);

    / *
     * Variable string
     *
     * NSMutableString
     *
     * /

    NSMutableString * mutableStr1 = [[NSMutableString alloc] init];
    NSLog (@ "% @", mutableStr1);
    NSMutableString * mutableStr2 = [NSMutableString string];

    // Concatenation of variable strings
    [mutableStr1 appendString: @ "abcdef"];
    NSLog (@ "% @", mutableStr1);
    NSString * resultString = [mutableStr1 stringByAppendingString: @ "xxxx"];

    NSLog (@ "% @", resultString);
    NSLog (@ "% @", mutableStr1);

    // Another string stitching method
    // stringByAppendingFormat: appendFormat
    [mutableStr2 appendFormat: @ "wang +% d", 1001];
    NSLog (@ "% @", mutableStr2);

    // Delete the string
    [mutableStr2 deleteCharactersInRange: NSMakeRange (2, 4)];
    NSLog (@ "% @", mutableStr2);

    // Insert string
    // Insert the specified string before the given subscript (pre-interpolation)
    [mutableStr2 insertString: @ "123" atIndex: 2];
    NSLog (@ "% @", mutableStr2);

    // Replace string
    // Replace the specified range of characters according to the given string
    [mutableStr2 replaceCharactersInRange: NSMakeRange (0, 3) withString: @ "xxx"];
    NSLog (@ "% @", mutableStr2);

     / *
     * Exercise 1:
     *
     * Given an image file name, determine whether the string starts with
     * At the end of "png", if it is, replace it with "jpg", if not, then stitch ".jpg".
     * /

    // Immutable string
    NSString * practiceStr = @ "wang / zhen / gang / .png ";
     if ([practiceStr hasSuffix: @ "png"]) {
         NSLog (@ "% @", [practiceStr stringByReplacingOccurrencesOfString: @ "png" withString: @ "jpg"]);
     }
     else {
         NSLog (@ "% @", [practiceStr stringByAppendingString: @ ". Jpg"]);
     }

     // variable string
     NSMutableString * practiceMutableStr = [[NSMutableString alloc] initWithString: @ "wang.png"];
// [practiceMutableStr appendString: @ "wang.png"]; // Note that the initialization of NSMutableSring cannot use syntactic sugar

     if ([practiceMutableStr hasSuffix: @ "png"]) {
// NSLog (@ "% @", [practiceMutableStr stringByReplacingOccurrencesOfString: @ "png" withString: @ "jpg"]);
         [practiceMutableStr replaceCharactersInRange: [practiceMutableStr rangeOfString: @ "png"] withString: @ "jpg"];
     }
     else {
         [practiceMutableStr appendString: @ ". jpg"];
     }
     NSLog (@ "% @", practiceMutableStr);



     return 0;
}


Objective-c----NSString, nsmutablestring


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.