标签:des style color os io 文件 for ar
1,创建字符串,有@都是对象。
NSString *myString1 = @"My String One”;
将一个utf8编码的字符串实例化成对象
NSString *myString2 = [NSString stringWithUTF8String:"My String Two"];
第三种写法
NSString *myString3 = [[NSString alloc] initWithUTF8String:"My String Three”];
OC里面用stringWithFormat连接字符串,例如
int number = 4;
NSString *myString4 = [NSString stringWithFormat:@"My String %i", number];
具体代码
NSString *myString1 = @"My String One";
NSLog(@"myString1 = %@", myString1);
NSString *myString2 = [NSString stringWithUTF8String:"My String Two"];
NSLog(@"myString2 = %@", myString2);
NSString *myString3 = [[NSString alloc] initWithUTF8String:"My String Three"];
NSLog(@"myString3 = %@", myString3);
int number = 4;
NSString *myString4 = [NSString stringWithFormat:@"My String %i", number];
NSLog(@"myString4 = %@", myString4);
2,mac读取文件内容
首先要有文件。注意IOS有专门的方法指定路径,在ObjectiveC 文件操作里已经讲过。具体代码
NSString *filePathName = @"/Users/Shared/test";
NSError *fileError;
NSString *textFileContents = [NSString stringWithContentsOfFile:filePathName
encoding:NSUTF8StringEncoding
error:&fileError];
if(fileError.code == 0)
NSLog(@"textfile.txt contents: %@", textFileContents);
else
NSLog(@"error(%ld): %@", fileError.code, fileError.description);
encoding:NSUTF8StringEncoding这里注意解码方式,用书上的文件里有汉字的话会出现乱码。
3,IOS读取文件内容。
如上面所说,IOS在沙箱内运行,用下面代码可得到app路径。
具体代码,注意解码方式。
NSString *bundlePathName = [[NSBundle mainBundle] resourcePath];
NSString *filePathName = [NSString stringWithFormat:@"%@/textfile.txt", bundlePathName];
NSError *fileError;
NSString *textFileContents = [NSString stringWithContentsOfFile:filePathName
encoding:NSUTF8StringEncoding
error:&fileError];
if(fileError.code == 0)
NSLog(@"textfile.txt contents: %@", textFileContents);
else
NSLog(@"error(%ld): %@", (long)fileError.code, fileError.description);
4,mac上的写字符串....其实我们对象集合里面讲过了,只不过那边写进去的是数组,是xml存储的。
下面是具体代码,写入的是字符串。
NSString *filePathName = @"/Users/Shared/textfile.txt";
NSError *fileError;
NSString *textFileContents = @"Content generated from a Mac program.";
[textFileContents writeToFile:filePathName
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&fileError];
if(fileError.code == 0)
NSLog(@"textfile.txt was written successfully with these contents: %@",
textFileContents);
else
NSLog(@"error(%ld): %@", fileError.code, fileError.description);
5,IOS上写入文件。下面是具体代码
就是你在AppDelegate.m里面加上代码
@synthesize window = _window;
在didFinishLaunchingWithOptions方法里加上
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePathName = [NSString stringWithFormat:@"%@/textfile.txt", documentsDirectory];
NSError *fileError;
NSString *textFileContents = @"Content generated from an iOS app.";
[textFileContents writeToFile:filePathName
atomically:YES
encoding:NSStringEncodingConversionAllowLossy
error:&fileError];
if(fileError.code == 0)
NSLog(@"textfile.txt was written successfully with these contents: %@",
textFileContents);
else
NSLog(@"error(%d): %@", fileError.code, fileError.description);
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
我们这里找下路径,/User/guanliyang/Library/Application Support/iPhone Simulator/7.1/application/1A88…./Documents/里面会有testfile.txt文件,并且里面有写入的字符串。
路径是NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];指定的,我们文件操作里也详细讲解过。
6,比较字符串。直接上代码
NSString *myString1 = @"A";
NSString *myString2 = @"B";
NSString *myString3 = @"A";
BOOL isEqual = [myString1 isEqualToString:myString2];
if(isEqual)
NSLog(@"%@ is equal to %@", myString1, myString2);
else
NSLog(@"%@ is not equal to %@", myString1, myString2);
// 可直接做条件
if([myString1 isEqualToString:myString2])
NSLog(@"%@ is equal to %@", myString1, myString2);
else
NSLog(@"%@ is not equal to %@", myString1, myString2);
if([myString1 isEqualToString:myString3])
NSLog(@"%@ is equal to %@", myString1, myString3);
else
NSLog(@"%@ is not equal to %@", myString1, myString3);
NSString *name = @"Mr. John Smith, MD";
BOOL hasMrPrefix = [name hasPrefix:@"Mr"];
if(hasMrPrefix)
NSLog(@"%@ has the Mr prefix", name);
else
NSLog(@"%@ doesn‘t have the Mr prefix", name);
BOOL hasMDSuffix = [name hasSuffix:@"MD"];
if(hasMDSuffix)
NSLog(@"%@ has the MD suffix", name);
else
NSLog(@"%@ doesn‘t have the MD suffix", name);
NSString *alphabet = @"ABCDEFGHIJKLMONPQRSTUVWXYZ";
NSRange range = NSMakeRange(2, 3);
BOOL lettersInRange = [[alphabet substringWithRange:range] isEqualToString:@"CDE"];
if(lettersInRange)
NSLog(@"The letters CDE are in alphabet starting at position 2");
else
NSLog(@"The letters CDE aren‘t in alphabet starting at position 2”);
简单解释下,比较字符串用[myString1 isEqualToString:myString2],决不能用==
[name hasPrefix:@"Mr”]最前的字符,是以Mr开头么,[name hasSuffix:@"MD"]结尾字符。
然后NSRange range = NSMakeRange(2, 3);从第二个开始,取三个,,所以取出的值[alphabet substringWithRange:range]就是CDE。
就是说,这里讲解了如何从字符串内取某个字符串,,这里是根据位置取的。
7,处理字符串。
可变字符串
NSMutableString *myString = [[NSMutableString alloc] initWithCapacity:26];
分配26个字符空间
[myString setString:@"ABCDEFGHIJKLMONPQRSTUVWXYZ"];
[myString appendString:@", 0123456789”];
由于是可变字符串,appendString添加在字符串尾部。插入头部用insertString
[myString insertString:@"abcdefg, "
atIndex:0];
用deleteCharactersInRange删除某个位置的字符串。
NSRange range = NSMakeRange(9, 3);
[myString deleteCharactersInRange:range];
从第九个开始,删除三个。
替换字符串。
NSRange rangeOfString = [myString rangeOfString:myString];
[myString replaceOccurrencesOfString:@", "
withString:@"|"
options:NSCaseInsensitiveSearch
range:rangeOfString];
,替换成|.
rangeOfString用来指定整个字符串,也可以指定范围。
NSRange rangeToReplace = NSMakeRange(0, 4);
[myString replaceCharactersInRange:rangeToReplace
withString:@"MORE"];
全部代码。
NSMutableString *myString = [[NSMutableString alloc] initWithCapacity:26];
[myString setString:@"ABCDEFGHIJKLMONPQRSTUVWXYZ"];
NSLog(@"%@", myString);
[myString appendString:@", 0123456789"];
NSLog(@"%@", myString);
[myString insertString:@"abcdefg, "
atIndex:0];
NSLog(@"%@", myString);
NSRange range = NSMakeRange(9, 3);
[myString deleteCharactersInRange:range];
NSLog(@"%@", myString);
NSRange rangeOfString = [myString rangeOfString:myString];
[myString replaceOccurrencesOfString:@", "
withString:@"|"
options:NSCaseInsensitiveSearch
range:rangeOfString];
NSLog(@"%@", myString);
NSRange rangeToReplace = NSMakeRange(0, 4);
[myString replaceCharactersInRange:rangeToReplace
withString:@"MORE"];
NSLog(@"%@", myString);
8,搜索字符串。
具体代码。
NSString *stringToSearch = @"This string is something that you can search.";
NSRange rangeToSearch = [stringToSearch rangeOfString:stringToSearch];
NSRange resultsRange = [stringToSearch rangeOfString:@"something"
options:NSCaseInsensitiveSearch
range:rangeToSearch];
if(resultsRange.location != NSNotFound){
NSLog(@"String found starting at location %lu with a length of %lu",
resultsRange.location, resultsRange.length);
NSLog(@"%@", [stringToSearch substringWithRange:resultsRange]);
}
else
NSLog(@"The search didn‘t turn up any results”);
9,本地化,专门做过笔记,而且比他详细,这里就不讲了。
10,数字转换成字符串。
float fNumber = 12;
NSString *floatToString = [NSString stringWithFormat:@"%f", fNumber];
NSLog(@"floatToString = %@", floatToString);
NSNumber *number = [NSNumber numberWithFloat:30];
NSString *numberToString = [number stringValue];
NSLog(@"numberToString = %@", numberToString);
一个stringWithFormat,一个stringValue两种方法。
11,字符串转数字。
NSString *aFloatValue = @"12.50";
float f = [aFloatValue floatValue];
float result = f * 2 + 45;
NSLog(@"f = %f and result = %f", f, result);
NSNumber *aFloatNumber = [NSNumber numberWithFloat:[aFloatValue floatValue]];
NSLog(@"aFloatNumber = %@", aFloatNumber);
也简单,一个floatValue一个numberWithFloat。
12,格式化数字,初始化数字对象。
NSNumber *numberToFormat = [NSNumber numberWithFloat:9.99];
NSLog(@"numberToFormat = %@", numberToFormat);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSLog(@"Formatted for currency: %@", [numberFormatter stringFromNumber:numberToFormat]);
这里风格有好几种样式,例如NSNumberFormatterPercentStyle,百分号,NSNumberFormatterScientificStyle科学计数,NSNumberFormatterNoStyle取整等等。
标签:des style color os io 文件 for ar
原文地址:http://www.cnblogs.com/guanliyang/p/3914636.html