NSString of Objective-C syntax

Source: Internet
Author: User

NSString of Objective-C syntax

// Dynamically insert a string in the string

// Because the returned link "www.yousawang.com" is missing "/", add "/" to "yousawang.com "/"

NSMutableString * string_logo = [NSMutableStringstringWithString: user. Store_logo];

Nsange range = [string_logorangeOfString: @ "yousawang.com"];

[String_logoinsertString: @ "/" atIndex: range. location + 13];

NSLog (@ "str = % @", string_logo );


// 1. NSString

// 1. Create a constant string.

NSString * astring = @ "This is a String! ";

// 2. Create an empty string and assign a value.

NSString * astring = [[NSString alloc] init];

Astring = @ "This is a String! ";

[Astring release];

NSLog (@ "astring: % @", astring );

//

NSString * astring = [[NSString alloc] init];

NSLog (@ "0x %. 8x", astring );

Astring = @ "This is a String! ";

NSLog (@ "0x %. 8x", astring );

[Astring release];

NSLog (@ "astring: % @", astring );

// 3. In the preceding method, the speed is improved by using the initWithString method.

NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "];

NSLog (@ "astring: % @", astring );

[Astring release];

// 4. Use Standard c to create a string: initWithCString Method

Char * Cstring = "This is a String! ";

NSString * astring = [[NSString alloc] initWithCString: Cstring];

NSLog (@ "astring: % @", astring );

[Astring release];

// 5. Create a formatted string: A placeholder (consisting of one % and one character)

Int I = 1;

Int j = 2;

NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "% d. This is % I string! ", I, j];

NSLog (@ "astring: % @", astring );

[Astring release];

// 6. Create a temporary string

NSString * astring;

Astring = [NSString stringWithCString: "This is a temporary string"];

NSLog (@ "astring: % @", astring );


NSString * path = @ "astring. text ";

NSString * astring = [[NSString alloc] initWithContentsOfFile: path];

NSLog (@ "astring: % @", astring );

[Astring release];


NSString * astring = [[NSString alloc] initWithString: @ "This is a String! "];

NSLog (@ "astring: % @", astring );

NSString * path = @ "astring. text ";

[Astring writeToFile: path atomically: YES];

[Astring release];

// Compare the strcmp function with C

Char string1 [] = "string! ";

Char string2 [] = "string! ";

If (strcmp (string1, string2) = 0)

{

NSLog (@ "1 ");

}


// IsEqualToString 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 );

// Determine whether the content of NSOrderedSame 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 objects (which is compared alphabetically. astring02 is true if it is greater than astring01)


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)

// Compare string 1 case-insensitive

NSString * astring01 = @ "this is a String! ";

NSString * astring02 = @ "This is a String! ";

BOOL result = [astring01 caseInsensitiveCompare: astring02] = NSOrderedSame;

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)


// Compare string 2 without case sensitivity

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 of a string, not the character value.


NSString * string1 = @ "A String ";

NSString * string2 = @ "String ";

NSLog (@ "string1: % @", [string1 uppercaseString]); // uppercase

NSLog (@ "string2: % @", [string2 lowercaseString]); // lower case

NSLog (@ "string2: % @", [string2 capitalizedString]); // initial size


NSString * string1 = @ "This is a string ";

NSString * string2 = @ "string ";

Nsange range = [string1 rangeOfString: string2];

Int location = range. location;

Int leight = range. length;

NSString * astring = [[NSString alloc] initWithString: [NSString stringWithFormat: @ "Location: % I, Leight: % I", location, leight];

NSLog (@ "astring: % @", astring );

[Astring release];


//-SubstringToIndex: captures the string from the beginning to the specified position, but does not include the characters at the position.

NSString * string1 = @ "This is a string ";

NSString * string2 = [string1 substringToIndex: 3];

NSLog (@ "string2: % @", string2 );



//-SubstringFromIndex: starts with a specified position (including the characters at the specified position) and includes all subsequent characters.

NSString * string1 = @ "This is a string ";

NSString * string2 = [string1 substringFromIndex: 3];

NSLog (@ "string2: % @", string2 );


//-SubstringWithRange: // captures the substring from the string based on the given position and length.

NSString * string1 = @ "This is a string ";

NSString * string2 = [string1 substringWithRange: NSMakeRange (0, 4)];

NSLog (@ "string2: % @", string2 );


// Extension 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]);


// StringWithCapacity:

NSMutableString * String;

String = [NSMutableString stringWithCapacity: 40];


// AppendString: and appendFormat:


NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];

// [String1 appendString: @ ", I will be adding some character"];

[String1 appendFormat: [NSString stringWithFormat: @ ", I will be adding some character"];

NSLog (@ "String1: % @", String1 );

*/


//-InsertString: atIndex:

NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];

[String1 insertString: @ "Hi! "AtIndex: 0];

NSLog (@ "String1: % @", String1 );


//-SetString:

NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];

[String1 setString: @ "Hello Word! "];

NSLog (@ "String1: % @", String1 );


//-SetString:

NSMutableString * String1 = [[NSMutableString alloc] initWithString: @ "This is a NSMutableString"];

[String1 replaceCharactersInRange: NSMakeRange (0, 4) withString: @ "That"];

NSLog (@ "String1: % @", String1 );


// 01: Check whether the string starts with another string-(BOOL) hasPrefix: (NSString *) aString;

NSString * String1 = @ "NSStringInformation.txt ";

[String1 hasPrefix: @ "NSString"] = 1? NSLog (@ "YES"): NSLog (@ "NO ");

[String1 hasSuffix: @ ". txt"] = 1? NSLog (@ "YES"): NSLog (@ "NO ");


// 02: Find whether the string contains another string-(nsange) rangeOfString: (NSString *) aString. This is previously used to search for substrings in the string;



// NSArray * array = [[NSArray alloc] initWithObjects:

@ "One", @ "Two", @ "Three", @ "Four", nil];


Self. dataArray = array;

[Array release];


//-(Unsigned) Count; number of objects contained in the array;

NSLog (@ "self. dataArray cound: % d", [self. dataArray count]);


//-(Id) objectAtIndex: (unsigned int) index; get the object at the specified index;

NSLog (@ "self. dataArray cound 2: % @", [self. dataArray objectAtIndex: 2]);



// ArrayWithArray:

// NSArray * array1 = [[NSArray alloc] init];

NSMutableArray * MutableArray = [[NSMutableArray alloc] init];

NSArray * array = [NSArray arrayWithObjects:

@ "A", @ "B", @ "c", nil];

NSLog (@ "array: % @", array );

MutableArray = [NSMutableArray arrayWithArray: array];

NSLog (@ "MutableArray: % @", MutableArray );


Array1 = [NSArray arrayWithArray: array];

NSLog (@ "array1: % @", array1 );



// Copy


// Id obj;

NSMutableArray * newArray = [[NSMutableArray alloc] init];

NSArray * oldArray = [NSArray arrayWithObjects:

@ "A", @ "B", @ "c", @ "d", @ "e", @ "f", @ "g", @ "h ", nil];


NSLog (@ "oldArray: % @", oldArray );

For (int I = 0; I <[oldArray count]; I ++)

{

Obj = [[oldArray objectAtIndex: I] copy];

[NewArray addObject: obj];

}

//

NSLog (@ "newArray: % @", newArray );

[NewArray release];


// Quick Enumeration

// NSMutableArray * newArray = [[NSMutableArray alloc] init];

NSArray * oldArray = [NSArray arrayWithObjects:

@ "A", @ "B", @ "c", @ "d", @ "e", @ "f", @ "g", @ "h ", nil];

NSLog (@ "oldArray: % @", oldArray );

For (id obj in oldArray)

{

[NewArray addObject: obj];

}

//

NSLog (@ "newArray: % @", newArray );

[NewArray release];


// Deep copy


// NSMutableArray * newArray = [[NSMutableArray alloc] init];

NSArray * oldArray = [NSArray arrayWithObjects:

@ "A", @ "B", @ "c", @ "d", @ "e", @ "f", @ "g", @ "h ", nil];

NSLog (@ "oldArray: % @", oldArray );

NewArray = (NSMutableArray *) CFPropertyListCreateDeepCopy (kCFAllocatorDefault, (CFPropertyListRef) oldArray, kCFPropertyListMutableContainers );

NSLog (@ "newArray: % @", newArray );

[NewArray release];


// Copy and sort


// NSMutableArray * newArray = [[NSMutableArray alloc] init];

NSArray * oldArray = [NSArray arrayWithObjects:

@ "B", @ "a", @ "e", @ "d", @ "c", @ "f", @ "h", @ "g ", nil];

NSLog (@ "oldArray: % @", oldArray );

NSEnumerator * enumerator;

Enumerator = [oldArray objectEnumerator];

Id obj;

While (obj = [enumerator nextObject])

{

[NewArray addObject: obj];

}

[NewArray sortUsingSelector: @ selector (compare :)];

NSLog (@ "newArray: % @", newArray );

[NewArray release];


// Split the string into an array-componentsSeparatedByString:

NSString * string = [[NSString alloc] initWithString: @ "One, Two, Three, Four"];

NSLog (@ "string: % @", string );

NSArray * array = [string componentsSeparatedByString: @ ","];

NSLog (@ "array: % @", array );

[String release];


// Merge elements from the array to the string-componentsJoinedByString:

NSArray * array = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three", @ "Four", nil];

NSString * string = [array componentsJoinedByString: @ ","];

NSLog (@ "string: % @", string );


// NSArray * array;

Array = [NSMutableArray arrayWithCapacity: 20];


//-(Void) addObject: (id) anObject;

// NSMutableArray * array = [NSMutableArray arrayWithObjects:

@ "One", @ "Two", @ "Three", nil];

[Array addObject: @ "Four"];

NSLog (@ "array: % @", array );


//-(Void) removeObjectAtIndex: (unsigned) index;

// NSMutableArray * array = [NSMutableArray arrayWithObjects:

@ "One", @ "Two", @ "Three", nil];

[Array removeObjectAtIndex: 1];

NSLog (@ "array: % @", array );


//-(NSEnumerator *) objectEnumerator; forward and backward

// NSMutableArray * array = [NSMutableArray arrayWithObjects:

@ "One", @ "Two", @ "Three", nil];

NSEnumerator * enumerator;

Enumerator = [array objectEnumerator];


Id thingie;

While (thingie = [enumerator nextObject]) {

NSLog (@ "thingie: % @", thingie );

}


//-(NSEnumerator *) reverseObjectEnumerator; forward from the back

// NSMutableArray * array = [NSMutableArray arrayWithObjects:

@ "One", @ "Two", @ "Three", nil];

NSEnumerator * enumerator;

Enumerator = [array reverseObjectEnumerator];


Id object;

While (object = [enumerator nextObject]) {

NSLog (@ "object: % @", object );

}


// Quick Enumeration

// NSMutableArray * array = [NSMutableArray arrayWithObjects:

@ "One", @ "Two", @ "Three", nil];

For (NSString * string in array)

{

NSLog (@ "string: % @", string );

}


//-(Id) initWithObjectsAndKeys;


// NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: @ "One", @ "1", @ "Two", @ "2", @ "Three ", @ "3", nil];

NSString * string = [dictionary objectForKey: @ "One"];

NSLog (@ "string: % @", string );

NSLog (@ "dictionary: % @", dictionary );

[Dictionary release];


// Create

NSMutableDictionary * dictionary = [NSMutableDictionary dictionary];


// Add a dictionary

[Dictionary setObject: @ "One" forKey: @ "1"];

[Dictionary setObject: @ "Two" forKey: @ "2"];

[Dictionary setObject: @ "Three" forKey: @ "3"];

[Dictionary setObject: @ "Four" forKey: @ "4"];

NSLog (@ "dictionary: % @", dictionary );


// Delete the specified dictionary

[Dictionary removeObjectForKey: @ "3"];

NSLog (@ "dictionary: % @", dictionary );





// Put NSRect into NSArray

NSMutableArray * array = [[NSMutableArray alloc] init];

NSValue * value;

CGRect rect = CGRectMake (0, 0,320,480 );

Value = [NSValue valueWithBytes: & rect objCType: @ encode (CGRect)];

[Array addObject: value];

NSLog (@ "array: % @", array );


// Extract from Array

Value = [array objectAtIndex: 0];

[Value getValue: & rect];

NSLog (@ "value: % @", value );


// NSFileManager * fileManager = [NSFileManager defaultManager];

NSString * home;

Home = @ "../Users /";


NSDirectoryEnumerator * direnum;

Direnum = [fileManager enumeratorAtPath: home];


NSMutableArray * files = [[NSMutableArray alloc] init];


// Enumeration

NSString * filename;

While (filename = [direnum nextObject]) {

If ([[filename pathExtension] hasSuffix: @ "jpg"]) {

[Files addObject: filename];

}

}


// Quick Enumeration

// For (NSString * filename in direnum)

//{

// If ([[filename pathExtension] isw.tostring: @ "jpg"]) {

// [Files addObject: filename];

//}

//}

NSLog (@ "files: % @", files );


// Enumeration

NSEnumerator * filenum;

Filenum = [files objectEnumerator];

While (filename = [filenum nextObject]) {

NSLog (@ "filename: % @", filename );

}


// Quick Enumeration

// For (id object in files)

//{

// NSLog (@ "object: % @", object );

//}


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.