• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-c-NSString

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
        //NSString stringWithFormat, stringWithString, stringWithUTF8String
NSString *s1 = [NSString stringWithFormat:@"苹果fans"];
NSLog(@"s1: %@", s1);

NSString *s2 = [NSString stringWithString:@"上海 developer"];
NSLog(@"s2: %@", s2);

NSString *s3 = [NSString stringWithUTF8String:"我是utf8字符串类型"];
NSLog(@"s3: %@", s3);


//NSString stringByAppendingFormat
int y = 2012;
char *c = "learn iOS"; //不能为中文
NSString *s4 = [NSString stringWithFormat:@"标记"];
NSString *s5 = [s4 stringByAppendingFormat:@"%d年%s", y, c];
NSLog(@"s5: %@", s5);


//NSString length, traverse
NSString *s6 = [NSString stringWithFormat:@"苹果iOS"];
NSInteger len = [s6 length];
NSLog(@"s6's length is %ld", len);

for(int i=0; i <len; i++){
char c1 = [s6 characterAtIndex:i];
NSLog(@"s6 - index %d, char is %c", i, c1);
}


//NSString isEqualToString
NSString *s7 = [NSString stringWithFormat:@"google"];
NSString *s8 = [NSString stringWithFormat:@"google"];
NSString *s9 = [NSString stringWithFormat:@"apple"]; //microsoft, Google

if ([s7 isEqualToString:s8]) {
NSLog(@"s7,s8: same");
}else
NSLog(@"s7,s8: not same");


//NSString compare NSCaseInsensitiveSearch, NSNumbericSearch, NSLiteralSearch
switch ([s7 compare:s9
options:NSCaseInsensitiveSearch]) {
case NSOrderedSame:
NSLog(@"s7 compare to s9: same");
break;
case NSOrderedAscending:
NSLog(@"s7 compare to s9: asc");
break;
case NSOrderedDescending:
NSLog(@"s7 compare to s9: desc");
break;
}

//NSString hasPrefix, hasSuffix
NSString *str10 = [NSString stringWithFormat:@"apple 粉丝"];
if ([str10 hasPrefix:@"apple"]) {
NSLog(@"str10 has prefix \"apple\"");
}else
NSLog(@"str10 has no prefix \"apple\"");

if ([str10 hasSuffix:@"粉丝"]) {
NSLog(@"str10 has suffix \"粉丝\"");
}else
NSLog(@"str10 has no suffix \"粉丝\"");

//NSString substringToIndex, substringFromIndex, substringWithRange
NSString *str11 = [NSString stringWithFormat:@"我I am an apple 粉丝"];
NSString *str12 = [str11 substringToIndex:5];
NSLog(@"str12: %@", str12);
NSString *str13 = [str12 substringFromIndex:1];
NSLog(@"str13: %@", str13);
NSString *str14 = [str11 substringWithRange:[str11 rangeOfString:@"apple"]];
NSLog(@"str14: %@", str14);


//NSString uppercaseString, lowercaseString, capitalizedString
NSString *str15 = [NSString stringWithFormat:@"apple FANS"];
NSString *str16 = [str15 uppercaseString];
NSString *str17 = [str15 lowercaseString];
NSString *str18 = [str15 capitalizedString];
NSLog(@"str16: %@", str16);
NSLog(@"str17: %@", str17);
NSLog(@"str18: %@", str18);

//NSString stringByReplacingOccurrencesOfString, stringByReplacingCharactersInRange
NSString *str19 = [NSString stringWithFormat:@"orange, apple, banana, pear"];
NSString *str20 = [str19 stringByReplacingOccurrencesOfString:@"," withString:@";"];
NSLog(@"str20: %@", str20);

NSRange range = [str19 rangeOfString:@"apple"];
NSLog(@"location is %ld, length is %ld", range.location, range.length);
NSString *str21 = [str19 stringByReplacingCharactersInRange:range
withString:@"peach"];
NSLog(@"str21: %@", str21);

//NSString 强制转换
NSString *s22 = @"2012";
NSInteger year1 = [s22 integerValue];
NSLog(@"NSInteger: %ld", year1);

int year2 = [s22 intValue];
NSLog(@"int: %d", year2);

s1 = @"3.1415926";
double d = [s1 doubleValue];
NSLog(@"double: %f", d);

float f = [s1 floatValue];
NSLog(@"float: %f", f);

//NSNotFound
NSString *str23 =[NSString stringWithFormat:@"apple"];
NSRange range = [str23 rangeOfString:@"ibm"];

if (range.location == NSNotFound) {
NSLog(@"not found");
}else
{
NSLog(@"location: %ld", range.location);
}




1、初始化字符串一 

[[NSString alloc] initWithFormat:@"%d",10];

 

2、初始化字符串二

[NSString alloc] initWithCString:@"字符串内容"]

 

3、字符串的替换

注:将字符串中的参数进行替换

参数1:目标替换值

参数2:替换成为的值

参数3:类型为默认:NSLiteralSearch

参数4:替换的范围

[str replaceOccurrencesOfString:@"1" withString:@"222" options:NSLiteralSearch range:NSMakeRange(0, [str length])];

 

4、给字符串分配容量

NSMutableString *String;
String = [NSMutableString stringWithCapacity:40];


5、追加字符串

 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 appendFormat:[NSString stringWithFormat:@", I will be adding some character"]];

 

6、在已有字符串中按照所给出范围和长度删除字符

 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 deleteCharactersInRange:NSMakeRange(0, 5)];


7、在已有字符串后面在所指定的位置中插入给出的字符串

NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 insertString:@"Hi! " atIndex:0];


8、按照所给出的范围,和字符串替换的原有的字符

 NSMutableString *String1 = [[NSMutableString alloc] initWithString:@"This is a NSMutableString"];
[String1 replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"];


9、判断字符串内是否还包含别的字符串(前缀,后缀)

NSString *String1 = @"NSStringInformation.txt";
[String1 hasPrefix:@"NSString"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");
[String1 hasSuffix:@".txt"] = = 1 ? NSLog(@"YES") : NSLog(@"NO");


10、返回一个数组,包含已经由一个给定的分隔符分为接收器串。

- (NSArray*)componentsSeparatedByString:(NSString*)NString

参数
分离器
分隔符的字符串。

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];
//listItems包含了:{ @"Norman", @"Stanley", @"Fletcher" }.

 

11、是否包含该字符串

NSRange range = [@"字符串--A" rangeOfString:“是否包含--B”];

if (range.location == NSNotFound)

{//不包含
}
else
{//包含
}


转:http://blog.sina.com.cn/s/blog_4adf31ea0100nlmn.html


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C内存布局发布时间:2022-07-18
下一篇:
OBJECTIVEC(XCODE)绘图功能简介(转载)发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap