day 4---文字列の詳細
5244 ワード
一、文字列の作成と変換1.オブジェクトメソッドによる1)新しい文字列NSString*str 2=[[NSString alloc]initWithString:str 1]を1つの文字列で作成します.2)C文字列をOC文字列NSString*str 3=[[NSString alloc]initWithUTF 8 String:[WHIOS 1603]];3)【アクセント】文字列NSString*str 4=[[NSString alloc]initWithFormat:@"%d-%2 f-%c-%s",100,3.1415926,'Q',"QF";
二、文字列の検索と抽出1)文字列検索NSRange=[str 1 rangeOfString:subStr 1];NSLog(@"location:%ld length:%ld", range.location,range.length);//NSRangeは構造体であり、親列におけるlocationとlengthの2つの部分//location子列の開始位置length子列の長さを含む
NSString *subStr2 = [str1 substringFromIndex:8];//8は下付き8の位置から切り取りを表します
//NSRange rg = NSMakeRange(4, 6); NSRange rg = {4,6}; NSString *subStr4 = [str1 substringWithRange:rg];
三、文字列サイズ比較と大文字小文字変換1)二つの文字列が等しいかどうかを判断する(非常に重要)BOOL bl=[str 1 isEqualToString:str 2]//bool値を返して判断する
NSString*newStr 2=[string 2 stringByAppendingFormat:@"%d,%@",999,@"バラ";NSLog(@"mewStr2 = %@", newStr2);
四、可変文字列1)空の文字列NSMutablesString*mStr 1=[[NSMutablesString alloc]init]を作成する.NSMutableString *mStr2 =[NSMutableString string];
2.
1)
NSString *str2 = [[NSString stringWithString:str1];
2) C OC
NSString *str3 = [NSString stringWithUTF8String:"WHiOS1603"];
3)【 】
( )
NSString *str4 = [NSString stringWithFormat:@"%d-%.2f-%c-%s", 100,3.1415926,'Q',"QF"];
3. OC
1) int
int a = str.intValue; %d
2) long NSInteger 32->int 64->long
NSInteger integer = str.intValue 32 %d ,64 %ld
3) BOOL
BOOL bl = str.boolValue; %d
4) OC C
const char *s = [str UTF8String]; %s
5)
NSUInteger len = [str length]; 32->unsigned int 64->unsigned long
6) ,unichar C char
unichar c = [str characterAtIndex:5];
二、文字列の検索と抽出1)文字列検索NSRange=[str 1 rangeOfString:subStr 1];NSLog(@"location:%ld length:%ld", range.location,range.length);//NSRangeは構造体であり、親列におけるlocationとlengthの2つの部分//location子列の開始位置length子列の長さを含む
2)
//
NSString *subStr2 = [str1 substringFromIndex:8];//8は下付き8の位置から切り取りを表します
// , ( : )
NSString *subStr3 = [str1 substringToIndex:4];
//
//NSRange rg = NSMakeRange(4, 6); NSRange rg = {4,6}; NSString *subStr4 = [str1 substringWithRange:rg];
三、文字列サイズ比較と大文字小文字変換1)二つの文字列が等しいかどうかを判断する(非常に重要)BOOL bl=[str 1 isEqualToString:str 2]//bool値を返して判断する
2)
NSComparisonResult result = [str1 compare:str2]; // ,
if (result == NSOrderedAscending) {
NSLog(@"str1 str2");
}else if(result == NSOrderedSame){
NSLog(@"str1 str2");
}else{
NSLog(@"str1 str2");
}
3)
NSComparisonResult result2 = [str1 caseInsensitiveCompare:str2];
if (result2 == NSOrderedAscending) {
NSLog(@"str1 str2");
}else if(result2 == NSOrderedSame){
NSLog(@"str1 str2");
}else{
NSLog(@"str1 str2");
}
4)
NSString *string = @"abcd123xyz";
//
NSString *uppStr = [string uppercaseString];
NSLog(@"string = %@", uppStr);
//
NSString *lowStr = [uppStr lowercaseString];
NSLog(@"loeStr = %@", lowStr);
// ,
NSString *capStr = [string capitalizedString];
NSLog(@"capStr = %@", capStr);
5)
NSString *string1 = @"qianfeng1603Class.txt";
NSString *preStr = @"qian";
NSString *suffStr = @".txt";
//1.
BOOL rect1 = [string1 hasPrefix:preStr];
if (rect1 ) {
NSLog(@" ");
}else{
NSLog(@" ");
}
//2.
BOOL rect2 = [string1 hasSuffix:suffStr];
if (rect2) {
NSLog(@" ");
}else{
NSLog(@" ");
}
6)
NSString *string2 = @"Hello,Girl!";
NSString *newStr1 = [string2 stringByAppendingString:@" I Love You!!!"];
NSLog(@"newStr1 = %@", newStr1);
//2)
NSString*newStr 2=[string 2 stringByAppendingFormat:@"%d,%@",999,@"バラ";NSLog(@"mewStr2 = %@", newStr2);
//3) ( /)
NSString *newStr3 = [string2 stringByAppendingPathComponent:@"love.ppt"];
NSLog(@"newStr3 = %@",newStr3);
四、可変文字列1)空の文字列NSMutablesString*mStr 1=[[NSMutablesString alloc]init]を作成する.NSMutableString *mStr2 =[NSMutableString string];
NSMutableString *mStr3 = [[NSMutableString alloc] initWithCapacity:20];
NSMutableString *mStr4 = [NSMutableString stringWithCapacity:10];
NSString *str =@" ";
NSMutableString *mStr5 = [[NSMutableString alloc] initWithString:str];
NSMutableString *mStr6 = [NSMutableString stringWithString:str];
2)
//1.
[mStr6 insertString:@" " atIndex:2];
NSLog(@"mStr6 = %@", mStr6);
//2.
[mStr6 appendString:@"- "];
NSLog(@"mStr6 = %@", mStr6);
//3.
[mStr6 appendFormat:@"%s"," NSMutableString"];
NSLog(@"mStr6 = %@", mStr6);
3)
//1.
NSRange range ={6,3};
[mStr6 deleteCharactersInRange:range];
NSLog(@"mStr6 = %@", mStr6);
4)
//1
NSRange range1 = {0,2};
[mStr6 replaceCharactersInRange:range1 withString:@" "];
NSLog(@"mStr6 = %@", mStr6);
//2.
NSString *newStr = @" ";
[mStr6 setString:newStr];
NSLog(@"mStr6 = %@", mStr6);