NSString Intercept string

Source: Internet
Author: User
Tags strcmp string to file

Common methods for NSString strings
2010-09-06 14:18
/*******************************************************************************************
NSString
*******************************************************************************************/
First, NSString
/*----------------method to create a string----------------*/

1. Create a constant string.
NSString *astring = @ "This is a string!";


2, create an empty string, give the assignment.

NSString *astring = [[NSString alloc] init];
Astring = @ "This is a string!";
[Astring release];
NSLog (@ "astring:%@", astring);


NSString *astring = [[NSString alloc] init];
NSLog (@ "0x%.8x", astring);
[Email protected] "This is a string!";
NSLog (@ "0x%.8x", astring);
[Astring release];
NSLog (@ "astring:%@", astring);




3, in the above method, lifting speed: Initwithstring method

NSString *astring = [[NSString alloc] initwithstring:@ "This is a string!"];
NSLog (@ "astring:%@", astring);
[Astring release];



4. Create a string with standard C: Initwithcstring method

Char *cstring = "This is a string!";
NSString *astring = [[NSString alloc] initwithcstring:cstring];
NSLog (@ "astring:%@", astring);
[Astring release];



5. Create a formatted string: placeholder (composed of one% plus 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);




/*----------------read string from File: Initwithcontentsoffile method----------------*/

NSString *path = @ "Astring.text";
NSString *astring = [[NSString alloc] initwithcontentsoffile:path];
NSLog (@ "astring:%@", astring);
[Astring release];


/*----------------Write String to 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];




/*----------------Compare two strings----------------*/

Compare by C: strcmp function

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 isequaltostring: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 judge 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 two object values (in alphabetical order, astring02 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 two object values (in alphabetical order, ASTRING02 is less than astring01 true)



Do not consider case 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 determines the size of two object values (in alphabetical order, ASTRING02 is less than astring01 true)



Do not consider case comparison 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: Make a full comparison, case-sensitive nsnumericsearch: Compares the number of characters in a string, not the character value.


/*----------------Change the case of the string----------------*/

NSString *string1 = @ "A String";
NSString *string2 = @ "String";
NSLog (@ "string1:%@", [string1 uppercasestring]);//Uppercase
NSLog (@ "string2:%@", [string2 lowercasestring]);//lowercase
NSLog (@ "string2:%@", [string2 capitalizedstring]);//initial Letter size


/*----------------search for substrings in the string----------------*/

NSString *string1 = @ "This is a string";
NSString *string2 = @ "string";
Nsrange 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];


/*----------------Extract the substring----------------*/
-substringtoindex: Intercepts from the beginning of a string to the specified position, but not to the character at that position
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringtoindex:3];
NSLog (@ "string2:%@", string2);




-substringfromindex: Starts at the specified position (including the character at the specified position) and includes all subsequent characters
NSString *string1 = @ "This is a string";
NSString *string2 = [string1 substringfromindex:3];
NSLog (@ "string2:%@", string2);




-substringwithrange://intercept substrings arbitrarily from a string according to the given position, 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 name extension
NSString *path = @ "~/nsdata.txt";
NSLog (@ "extension:%@", [Path pathextension]);




/*******************************************************************************************
Nsmutablestring
*******************************************************************************************/

/*---------------assign capacity to string----------------*/
Stringwithcapacity:
Nsmutablestring *string;
String = [nsmutablestring stringwithcapacity:40];


/*---------------Add a character after an existing string----------------*/

Appendstring:and AppendFormat:

nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 appendstring:@], I'll be adding some character "];
[String1 appendformat:[nsstring stringwithformat:@ ", I'll be adding some character"]];
NSLog (@ "string1:%@", String1);
*/


/*--------Delete the character in the existing string according to the given range and length------*/
/*
Deletecharactersinrange:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 deletecharactersinrange:nsmakerange (0, 5)];
NSLog (@ "string1:%@", String1);



/*--------Insert the given string in the specified position after the existing string------*/

-insertstring:atindex:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 insertstring:@] hi! [atindex:0];
NSLog (@ "string1:%@", String1);



/*--------Swop an existing empty character into another string------*/

-setstring:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 setstring:@ "Hello word!"];
NSLog (@ "string1:%@", String1);



/*--------The original characters replaced by the given range, and the string------*/

-setstring:
nsmutablestring *string1 = [[Nsmutablestring alloc] initwithstring:@ "This is a nsmutablestring"];
[String1 replacecharactersinrange:nsmakerange (0, 4) withstring:@ "that"];
NSLog (@ "string1:%@", String1);



/*-------------Determine if the string also contains other strings (prefixes, suffixes)-------------*/
01: Check if 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 if String somewhere contains other string-(Nsrange) rangeofstring: (NSString *) astring, this point in the string before the search substring used;



/*******************************************************************************************
Nsarray
*******************************************************************************************/

/*---------------------------Create an array------------------------------*/
Nsarray *array = [[Nsarray alloc] Initwithobjects:
@ "One", @ "one", @ "three", @ "four", nil];

Self.dataarray = array;
[Array release];

-(unsigned) count; The number of objects contained in the array;
NSLog (@ "Self.dataarray cound:%d", [Self.dataarray Count]);

-(ID) Objectatindex: (unsigned int) index; Gets the object at the specified index;
NSLog (@ "Self.dataarray cound 2:%@", [Self.dataarray objectatindex:2]);


/*--------------------------copy data from one array to another (variable number)----------------------------*/

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];



/*---------------------------Cut SCORE Group------------------------------*/

Split from string to array-componentsseparatedbystring:
NSString *string = [[NSString alloc] initwithstring:@ "One,two,three,four"];
NSLog (@ "string:%@", string);
Nsarray *array = [string componentsseparatedbystring:@ ","];
NSLog (@ "array:%@", array);
[String release];


Merging elements from an array to a string-componentsjoinedbystring:
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil];
NSString *string = [Array componentsjoinedbystring:@ ","];
NSLog (@ "string:%@", string);



/*******************************************************************************************
Nsmutablearray
*******************************************************************************************/
/*---------------allocate capacity to the array----------------*/
Nsarray *array;
Array = [Nsmutablearray arraywithcapacity:20];



/*--------------Add an object at the end of the array----------------*/
-(void) AddObject: (ID) anobject;
Nsmutablearray *array = [Nsmutablearray arraywithobjects:
@ "One", @ "one", @ "three", nil];
[Array addobject:@ "four"];
NSLog (@ "array:%@", array);



/*--------------Delete the object at the specified index in the array----------------*/
-(void) Removeobjectatindex: (unsigned) index;
Nsmutablearray *array = [Nsmutablearray arraywithobjects:
@ "One", @ "one", @ "three", nil];
[Array removeobjectatindex:1];
NSLog (@ "array:%@", array);



/*-------------Array Enumeration---------------*/
-(Nsenumerator *) objectenumerator; front and back
Nsmutablearray *array = [Nsmutablearray arraywithobjects:
@ "One", @ "one", @ "three", nil];
Nsenumerator *enumerator;
Enumerator = [array objectenumerator];

ID thingie;
while (thingie = [Enumerator nextobject]) {
NSLog (@ "thingie:%@", thingie);
}


-(Nsenumerator *) Reverseobjectenumerator;
Nsmutablearray *array = [Nsmutablearray arraywithobjects:
@ "One", @ "one", @ "three", nil];
Nsenumerator *enumerator;
Enumerator = [array reverseobjectenumerator];

ID object;
while (object = [Enumerator Nextobject]) {
NSLog (@ "object:%@", object);
}


Quick Enumeration
Nsmutablearray *array = [Nsmutablearray arraywithobjects:
@ "One", @ "one", @ "three", nil];
For (NSString *string in array)
{
NSLog (@ "string:%@", string);
}



/*******************************************************************************************
Nsdictionary
*******************************************************************************************/

/*------------------------------------Create a dictionary------------------------------------*/
-(ID) Initwithobjectsandkeys;

Nsdictionary *dictionary = [[Nsdictionary alloc] initwithobjectsandkeys:@ "One", @ "1", @ "one", @ "2", @ "three", @ "3", nil ];
NSString *string = [Dictionary objectforkey:@ "One"];
NSLog (@ "string:%@", string);
NSLog (@ "dictionary:%@", dictionary);
[Dictionary release];


/*******************************************************************************************
Nsmutabledictionary
*******************************************************************************************/

/*------------------------------------Create a mutable dictionary------------------------------------*/
Create
Nsmutabledictionary *dictionary = [Nsmutabledictionary dictionary];

Add a dictionary
[Dictionary setobject:@ "One" forkey:@ "1"];
[Dictionary setobject:@ "," 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);


/*******************************************************************************************
Nsvalue (Wrapping any object)
*******************************************************************************************/

/*--------------------------------put Nsrect in Nsarray------------------------------------*/
Put the nsrect into the 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);

Extracting from array
Value = [array objectatindex:0];
[Value getvalue:&rect];
NSLog (@ "value:%@", value);


/*******************************************************************************************
Search for files with the extension jpg from the directory
*******************************************************************************************/

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] isequaltostring:@ "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);
//}


Nsrange range = [a rangeofstring:@ "$file/"];//Gets the location of the $file/

NSString *b = [a substringFromIndex:range.location + range.length];//start intercept

NSString Intercept string

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.