标签:
//
// main.m
// 字符串(NSString&NSMutableString)
//
// Created by 闫合 on 16/5/19.
// Copyright © 2016年 闫合. All rights reserved.
//
#pragma mark NSString 和 NSMutableString
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//初始化(工厂行为、对象初始化)
//行为
//Foundation框架下有两个字符串的类
//NSString静态类 NSMutableSting动态类
// NSString *str = @"123";
//类的行为
#pragma mark 通过c字符串来构建一个OC字符串
//可以通过c字符串来构建一个OC字符串
/* char *cstr = "你好!";
NSString *str1 = [NSString stringWithCharacters:cstr length:strlen(cstr)];
NSLog(@"%@",str1);*/
/* NSString *str = [NSString stringWithFormat:@"%d",123];//把数值转换为字符串“123”
NSLog(@"%@",str);
NSString *str1 = [NSString stringWithString:@"ASD"]; //@"ASD";
NSLog(@"%@",str1);
NSString *str2 = [NSString stringWithUTF8String:"中国"];
NSLog(@"%@",str2);*/
/* NSString *str = [[NSString alloc]initWithFormat:@"%d",123];
NSLog(@"%@",str);
NSString *str1 = [[NSString alloc]initWithString:@"123"];
NSLog(@"%@",str1);*/
#pragma mark 对字符串进行大小写处理
/* //对字符串进行大小写处理
NSString *str = @"abc";
NSLog(@"%@",str);
str = [str uppercaseString];//小写字母转大写
NSLog(@"%@",str);
NSString *str1 = @"aBc";
NSLog(@"%@",str1);
str1 = [str1 lowercaseString];//大写字母转小写
NSLog(@"%@",str1);*/
#pragma mark 判断字符串长度
/* //判断字符串长度
NSString *str = @"123456789";
NSLog(@"%lu",[str length]);//9
NSString *str1 = @"和123";
NSLog(@"%lu",[str1 lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);//在OC里一个汉字占三个字节*/
#pragma mark 判断字符串中是否含有某个字符串
/*
//判断字符串中是否有某个字符串
NSString *str = @"12.3";
NSRange range = [str rangeOfString:@"2."];
// NSLog(@"length = %lu,location = %lu",range.length,range.location);
if (range.location==NSNotFound)
{
NSLog(@"没有");
}
else
{
NSLog(@"有");
}
*/
#pragma mark 字符串内容比较
/*
//字符串内容比较
NSString *str = @"1234";
if ([str isEqualToString:@"1234"])
{
NSLog(@"是");
}else
{
NSLog(@"不是");
}
*/
#pragma mark 字符串转数值
/*//字符串转数值
NSString *str = @"123";
int s = [str intValue];//转整型
NSLog(@"%d",s);
double d = [str doubleValue];//转double
NSLog(@"%.2f",d);
*/
#pragma mark 判断字符串是否以字符串开头或结尾
// 判断字符串是否以字符串开头或结尾
/*
//是不是以“”开头的
NSString *str = @"12.3";
BOOL b = [str hasPrefix:@"1"];
if (b)
{
NSLog(@"是");
}else
{
NSLog(@"不是");
}
//是不是以“”结尾的
NSString *str1 = @"张三丰";
BOOL b1 = [str1 hasSuffix:@"丰"];
if (b1)
{
NSLog(@"是");
}else
{
NSLog(@"不是");
}
*/
#pragma mark 提取子字符串
//提取子字符串
/*NSString *str = @"我们是中国人????";
NSString *str1 = [str substringFromIndex:6];//从哪个索引位置开始
NSLog(@"str1=%@",str1);
str1 = [str substringToIndex:1];
NSLog(@"str1=%@",str1);//到哪个索引位置结束
NSRange range = {0,2};
str1 = [str substringWithRange:range];//截取某个范围的字符串
NSLog(@"str1=%@",str1);
NSString *str = @"123456";//逆序输出
NSRange range = {0,1};
for (long i=[str length]-1; i>=0; i--)
{
range.location = i;
NSLog(@"%@",[str substringWithRange:range]);
}
*/
#pragma mark 剔除字符串两端的空格stringByTrimmingCharactersInSet
/*
NSString *str = @"123";
NSString *str1 = @" 456";
str1 = [str1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];//剔除字符串两端的空格
NSString *str2 = @"7";
NSLog(@"%@%@%@",str,str1,str2);
*/
#pragma mark 写文件到磁盘writeToFile,再读取文件
/*
// NSString *str = @"123中国";
// [str writeToFile:@"abc.txt" atomically:YES];//这种写文件的方法已经被弃用了,不推荐使用了
//[str writeToFile:@"abc.txt" atomically:NO encoding:NSUTF8StringEncoding error:nil];
NSString *str;
NSError *error;
str = [NSString stringWithContentsOfFile:@"abc.txt" encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@",error);
*/
#pragma mark 字符串扩展stringByAppendingString
/*
//字符串扩展stringByAppendingString
NSString *str = @"中国";
//str = [NSString stringWithFormat:@"%@%@",str,@"崛起"];
str = [str stringByAppendingString:@"崛起!"];
NSLog(@"%@",str);
*/
#pragma mark NSMutableSting动态类
NSMutableString *str = [NSMutableString stringWithCapacity:100];//预留100个字符的空间
NSLog(@"%@",str);//此时是空串
// [str appendString:@"123"];//直接添加字符串"123"
[str appendFormat:@"%d",123];//格式化的添加"123"
NSRange r = {0,2};
[str replaceCharactersInRange:r withString:@""];
NSLog(@"%@",str);
return 0;
}
}
标签:
原文地址:http://www.cnblogs.com/Yanhe-blogs/p/5509808.html