, &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &N Bsp "string"
The "note" OC program, which relies on the base Class Library Foundation.foundation framework provides a number of official classes, which have three basic data structures.
"String" "Array" "Dictionary"
Knowledge points
1.NSString
2.NSMutableString
*****************************************************************************
========= immutable String (nsstring) =========
1. Why do I need nsstring objects
A: When you create a string in OC, you generally do not use the C method.
Because C takes a string as an array of characters, there are many inconvenient things to do in the operation.
Some methods of nsstring integration in cocoa can easily manipulate strings.
string defined in 2.OC and C language definition string
Oc:nsstring *str = @ "AAA";
C:char *c = "AAA";
Char c[20] = "Hello";
3.NSLog How to format NSString objects
OC Language:%@
C Language:%s
4. String creation
(1) Define string constants:
NSString *str = @ "My name is xiaoming.";
@ "XXX" means to tell the compiler to create a string object, and the return value is the address of the object.
Only the official classes have such privileges.
(2) Create another string with one string
NSString * str2 = [[NSString alloc] initwithstring:str];
NSString * STR3 = [[NSString alloc] initwithstring:@ "I am a string of babbling!"];
(3) Convert C string to OC string
NSString * STR3 = [[NSString alloc] initwithutf8string: "I am the C string"];
(4) Creating a string with a format character (stitching a string)
NSString * STR4 = [[NSString alloc] initwithformat:@ "I am Magnum splicing string%d%c%.2f", 1, ' T ', 3.14];
[Note] Each Init method corresponds to a Stringwith class method
(5)
NSString *STR5 = [NSString stringwithstring:str];
(6)
NSString *STR6 = [NSString stringwithutf8string: "Qianfeng"];
(7)
NSString *STR7 = [NSString stringwithformat:@ "I am Magnum splicing string%d%c%.2f", 1, ' T ', 3.14];
Conversion of 5.C language strings and NSString
1. Convert c string to OC string
str = [NSString stringwithutf8string:cstring];
2. Convert OC string to C string
const char *c = [ocstring utf8string];
6. Find the string length (number of characters)
The length of the kanji is also 1
Nsuinteger ret = [str length];
7. Get the appropriate characters by index
Unichar c = [str characteratindex:i];
NSLog (@ "%c", C);
Unicode, which stores national characters with greater storage space.
The default encoding format for Mac is called the UTF-8 encoding format, which is a branch of the universal code, and the idea is that different characters are stored in different bytes. For example, a kanji account is 3 bytes, an English character is 1 bytes, but it is all a character.
8. String Judgments
1) Determine whether the contents of two strings are the same
Compares two strings for the same contents (case sensitive)
BOOL isEq = [str2 ISEQUALTOSTRING:STR3];
type bool: Yes is true (1), no is False (0)
if (isEq = = YES)
{
NSLog (@ "content is the same");
}
Else
{
NSLog (@ "content is not the same");
}
2) Determine whether two strings are the same object
if (str2 = = STR3)
{
NSLog (@ "Same Object");
}
Else
{
NSLog (@ "Not the same object");
}
9. String comparisons
Nscomparisonresult result = [str1 compare:str2];
Switch (Result)
{
Case nsorderedascending:
NSLog (@ "ascending");
Break
Case Nsorderedsame:
NSLog (@ "Same");
Break
Case nsordereddescending:
NSLog (@ "descending");
Break
Default
Break
}
10. Convert to basic type (Int,float,bool,double,integer)
(1) int [str1 Intvalue]
(2) Nsinteger [str1 IntegerValue]
(3) Float [str1 Floatvalue]
(4) BOOL [str1 boolvalue];
(5) Double [str1 Doublevalue]
(6) long long [str1 Longlongvalue]
11. Replacing strings
b string substitution A string (A is replaced by B)
[Str1 stringbyreplacingoccurrencesofstring:@ "A" withstring:@ "B"];
12. Find the location and range of substrings
Nsrange range = [str rangeofstring:@ "whitewashed"];
if (range.location = = Nsnotfound)
{
NSLog (@ "not found!");
}
Else
{
NSLog (@ "%lu%lu", Range.location, Range.length);
}
13. Extraction of sub-strings
(1) before starting from 0 to the specified position
NSString * subStr1 = [str substringtoindex:5];
(2) Starting from the specified subscript to the last
NSString * subStr2 = [str substringfromindex:5];
(3) Interception of any part
Nsrange range = {1,5};
NSString * SUBSTR3 = [str substringwithrange:range];
========= variable string (nsmutablestring) =========
Nsmutablestring inherited from NSString
1. All nsstring methods, nsmutablestring can be used
2. All parameters need to pass in NSString * Incoming nsmutablestring * also line
1. Initializing a mutable string (must be initialized)
(1) nsmutablestring *string = [[Nsmutablestring alloc] init];
(2) string = [nsmutablestring string];
2. Initializing a mutable string with a string
string = [nsmutablestring stringWithFormat:];
[nsmutablestring stringwithstring:];
3. Reset String Contents
[String setstring:@ "abbbb"];
4. Inserting a string according to the specified location
[String insertstring:@ "Atindex: Subscript" to be inserted];
5. Append a partial string (appended at the end)
(1) [String appendstring:@ "to append"];
(2) [String appendformat:@ "name=%@", @ "xiaoming"];
6. Remove part of a string
Find the range you want to delete
Nsrange range = [string rangeofstring:@ "22222"];
Delete
[String Deletecharactersinrange:range];
7. Modifying strings
[Str replacecharactersinrange:range withstring:@ "xxxxxxxxxxxxxxx"];
******************************************************************************
"Category/category"
———————————————————————————————————————
<1>: The wording of the category.
@interface class name (category name)
Defining methods
@end
"Note" Once the class is used to add methods to existing classes, the object of this class can use this method
<2> Category features:
1. Methods can be added to existing/system-native classes
2. Class methods can be categorized and managed so that the implementation of the class can be dispersed across multiple files or multiple different frameworks.
<3> issues to be aware of using categories
1. You cannot add a member variable in a category
2. Use the category increment method to import the category header file
3. Methods in the parent class category, subclasses can also use the
<4> class Extensions/anonymous categories
When you don't want to expose some of the class's methods, we can use the class extension
1. Basic syntax for class extensions (written in. m files)
@interface person ()//No Name
-(void) song;
@end
@implementation person
-(void) song{
}
@end
The declarations and implementations in the "note" class extension are placed in the. m file
2. function of class extension
1. Private methods can be implemented
2. Make it easy for programmers to call a method that is not public
3. You can declare member variables
Various methods and categories for OC_ strings