//
Main.m
// string acquisition
//
Created by admin on 15/8/13.
Copyright (c) years admin. All rights reserved.
//
#import <Foundation/Foundation.h>
/*
1. get the string length
2. get a single character based on a string index // the index here is subscript
3. obtain a string based on the index
4. sub-string to get the substring in the index position in a string
5. depending on the string. Get the range of a string
*/
int Main (int argc,const Char * argv[]) {
@autoreleasepool {
1. Get string length
nsstring *str1 = @ "www.baidu.com";
nsuinteger length = [str1 length]; //Nsuiteger represents an unsigned integer type
NSLog (@ "length =%lu", length);
/ * 2. To get a single character in a string by index
Note:
> The given index value must be between 0 and the length. No group causes an exception Range or index out of bounds
> Assume that the index value is negative. Then the obtained value is a negative number.
*/
Unichar c = [str1 characteratindex:0];
NSLog (@ " the string obtained is:%c", c);
//3. Get a substring of a string based on the index value
// start with the index. Contains the string following the index fetch
nsstring *tmpstr2 = [str1 substringfromindex:3];
// from the beginning of the string to the index position, not including the index
nsstring *tmpstr3 = [str1 substringtoindex:5];
//Nsrange is a struct, takes the range of a string, the first parameter starts from which index value, and the second parameter indicates how long it takes
Nsrange s = {6,3};
nsstring *TMPSTR4 = [str1 substringwithrange: s];
NSLog (@ "TMPSTR2 =%@", TMPSTR2);
NSLog (@ "TMPSTR3 =%@", TMPSTR3);
NSLog (@ "TMPSTR4 =%@", TMPSTR4);
//4. Gets the index position of the substring in another string
nsstring *TMPSTR5 = @ "Bai";
// find substrings in a string. and the index starting position location and length are deposited into the structure
nsrange tmprange = [str1 rangeofstring: TMPSTR5];
// Print separately
NSLog (@ "location =%lu, length =%lu", Tmprange. location, Tmprange. length);
// ability to convert a Range struct to a string using Nsstringfromrange
nsstring *tmpstr6 = nsstringfromrange(tmprange);
NSLog (@ "TMPSTR6 =%@", TMPSTR6);
// infer if found
if (tmprange. Location == Nsnotfound {//enum {nsnotfound = Nsintegermax};
NSLog(@ "TMPSTR5 not in str1");
}Else
{
NSLog (@ "rangeofstring%@", TMPSTR6);
}
//5. Get sub-string index range
// The first parameter is a substring, the second is the method of selecting the search
nsrange tmpRange2 = [str1rangeofstring:@ "com"options:/ C9>nscaseinsensitivesearch];
nsrange tmpRange3 = [str1 rangeofstring:@ "com"];
NSLog (@ "tmpRange2 =%@",nsstringfromrange(tmpRange2));
NSLog (@ "tmpRange3 =%@",nsstringfromrange(tmpRange3));
}
return 0;
}
Small white Learning Development (IOS) OC_ string Acquisition (2015-08-11)