//
// main.m
// NSString
//
// Created by New-World on 13-10-30.
// Copyright (c) 2013年 Gary. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "string.h"
#pragma mark NSString的创建
int main(int argc, const char * argv[])
{
@autoreleasepool {
// //--------------------------NSNumber--------------------//
// //创建一个NSNumber对象1.类方法 2.实例方法
// NSNumber *monthNumber=[NSNumber numberWithInt:10];
// NSNumber *lengthNumber=[[NSNumber alloc] initWithFloat:10.8];
// NSLog(@"monthNumber:%@",lengthNumber);
// //还原成基本类型
// int month=[monthNumber intValue];
// float length=[lengthNumber floatValue];
// NSLog(@"%i",month);
// NSLog(@"%f",length);
//创建了一个字符串常量
NSString *str0=@"这是一个字符串";
//创建了一个空字符串
NSString *str1=[[NSString alloc] init];
//NSString *str2=[NSString string];
//快速创建一个字符串 常量区
NSString *str3=[[NSString alloc] initWithString:@"apple"];
//NSString *str40=[NSString stringWithString:@"xyz"];
//NSString *str41=[NSString stringWithString:@"xyz"];
//创建一个格式化字符串 堆区
NSString *str5=[[NSString alloc] initWithFormat:@"a"];
NSString *str6=[[NSString alloc] initWithFormat:@"b"];
NSString *str7=[NSString stringWithFormat:@"%f",3.2];
NSString *str8=[NSString stringWithFormat:@"%f",3.2];
NSLog(@"%@ %@ %@",str0,str1,str8);
if([str7 isEqualToString:str8])//比较内容
{
NSLog(@"same content");
}
else{
NSLog(@"different content");
}
if (str7 == str8)//比较地址
{
NSLog(@"same pointer");
}
else{
NSLog(@"different pointer");
}
NSComparisonResult s=[str5 caseInsensitiveCompare:str6];//比较字符串的大小
NSLog(@"result: %ld",s);
//转换成基本数据类型
NSLog(@"%f",[str8 floatValue]);
//字符串的转换
NSLog(@"%@",[str3 uppercaseString]);//大写
NSLog(@"%@",[str3 lowercaseString]);//小写
NSLog(@"%@",[str3 capitalizedString]);//首字母大写
//字符串转换成数组
NSString *str=@"abc bdc dds";
NSArray *array=[str componentsSeparatedByString:@" "];
NSLog(@"%@",array);
//字符串的截取
NSString *str10=@"bacbcbcbc";
NSString *subString1=[str10 substringFromIndex:3];//从下标为3开始截取,不包含下标为3的字符
NSString *subString2=[str10 substringToIndex:3];//截取到下标为3的字符
NSLog(@"%@ %@",subString1,subString2);
NSRange rang;
rang.location=2;//截取的位置
rang.length=2;//截取的长度
NSString *string3=[str3 substringWithRange:rang];
NSLog(@"%@",string3);
//字符串的拼接
NSString *Pstr1=@"ni",*Pstr2=@"hao";
NSString *PWstr=[[NSString alloc] initWithFormat:@"这是拼接的字符串:%@%@",Pstr1,Pstr2 ];
NSString *PWstr1=[Pstr1 stringByAppendingFormat:@"%@",Pstr2];
NSString *PWstr2=[Pstr1 stringByAppendingString:Pstr2];
NSLog(@"%@ %@ %@",PWstr1,PWstr2,PWstr);
//字符串的查询
NSRange range=[str10 rangeOfString:@"ac"];
NSLog(@"%@",NSStringFromRange(range));
if(range.location!=NSNotFound)
{
NSLog(@"string 找到了");
}
//----------------------华丽的分割线----------------------
//可变字符串的操作
//1.插入字符串
NSMutableString *mu=[[NSMutableString alloc]initWithFormat:@"abcdefg"];
[mu insertString:@"oooo" atIndex:3];
NSLog(@"mu :%@",mu);
//替换字符串
[mu replaceCharactersInRange:NSMakeRange(0, 3) withString:@"asssss"];
NSLog(@"mu :%@",mu);
//删除字符串
[mu deleteCharactersInRange:NSMakeRange(1, 2)];
NSLog(@"mu :%@",mu);
}
return 0;
}
OC语法之NSString
最新推荐文章于 2020-07-28 21:27:40 发布