ios NSString

NSString* firstName = @"Albert";
NSString* lastName = @"Elinstein";
NSString* fullName = [firstName stringByAppendingString:lastName];

NSString* sentence = @"Welcome";
sentence = [sentence stringByAppendingString:@"to the "];

NSString* dayOfWeek = nil;
NSString* first = @"Monday";
dayOfWeek1 = [[NSString stringWithString:first];

===================================

+stringWithFormat:使用NSLog格式创建字符串
+stringWithUTF8String
+stringWithContentsOfFile:fullPath usedEncoding: &encoding error: &error //读取文件内容
-length
-characterAtIndex
-hasPrefix
-intValue
-UTF8String
-substringFromIndex
-substringWithRange
-rangeOfSubString
-lastPathComponent:返回文件名,/usr/local/host.conf //返回host.conf
-pathExtension: //返回文件扩展名,上述返回.conf
-stringByExpandingTildeInPath:// 返回绝对路径,比如unix-style的路径 ~username,返回/user/username
-stringByDeletingLastPathComponent://返回文件名,如上述返回host
-stringByAppendingPathComponent://在路径下添加文件名称
-stringByResolvingSymlinksInPath // 返回符号链接的绝对路径
-isAbslutePath //判断是否为绝对路径,比如/Developer/Xcode.app则返回true, usr/local/conf.c 则返回false
-writeToFile:fullPath atomically:NO encoding:NSASCIIStringEncoding error:&error];
 


========================
NSUserName() 返回unix-style的用户名  // scott
NSFullUserName() 返回用户的全名 // Scott Stevenson
NSHomeDirectory() 返回用户的家目录 // /Users/scott
NSTemporaryDirectory() 返回临时目录 // /temp
NSStringEncoding encoding;
==========================
typedef struct NSRange {
    NSUInteger location;
    NSUInteger length;
} NSRange;
=========================
NSString* fullName = @"Albert Einstein";
NSRange myRange = NSMakeRange(0,6) // Albert

fullName = @"Last name: Einstein";
NSRange fieldRange = [fullName rangeOfString:@"Last name: "];

int start = fieldRange.location;
int count = fieldRange.length;
int startOfName = start + count;
NSString* lastName = [fullName substringFromIndex: startOfName];//Einstein
==========================
NSString* totalString = @"10";
NSString* goldenRatioString = @"1.618";
NSString* cityName = @"Cuperino";

int total = [totalString intValue];
float goldenRatio = [goldenRatioString floatValue];
const char* cityNameCString = [cityName UTF8String];
------------------------------------------------------
int count = 100;
float piValue = 3.1415926;
char* starName = "Vega";

NSString* countStrin = [NSString stringWithFormat:@"%i", count];
NSString* piString = [NSString stringWithFormat:@"%f", piValue];
NSString* startNameString = [NSString stringWithUTF8String:starName];

===============================
NSString* firstString = [NSString stringWithFormat:@"%i", 10000];
NSString* secondString = [NSString stringWithFormat:@"%i", 10000];

if([firstString isEqualToString:secondString]) {
   
}

----------------------
NSString* firstCity = @"Cupertino";
NSString* secondCity = @"Cupertino";

if ( firstCity == secondCity ) {//the condition is true
}

=================================
-lastPathComponent:返回文件名,/usr/local/host.conf //返回host.conf
-pathExtension: //返回文件扩展名,上述返回.conf
-stringByExpandingTildeInPath:// 返回绝对路径,比如unix-style的路径 ~username,返回/user/username
-stringByDeletingLastPathComponent://返回文件名,如上述返回host
-stringByAppendingPathComponent://在路径下添加文件名称
-stringByResolvingSymlinksInPath // 返回符号链接的绝对路径
-isAbslutePath //判断是否为绝对路径,比如/Developer/Xcode.app则返回true, usr/local/conf.c 则返回false

NSString* pathToApp = @"/Applications/Safari.app";
NSString* fullFileName = [pathToApp lastPathComponent]; // Safari.app
NSString* fileName = [fullFileName stringByDeletingPathExtension]; //Safari

------------------------------


==============================================
// 将字符串写入文件
NSString* lastLine = @"Beauty is truth, truth beauty";
NSString* fileName = @"GrecianUrn.txt";
NSString* homeDir = NSHomeDirectory();
NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];

[lastLine writeToFile:fullPath atomically:NO]; // atomically:如果为true,则先将文件写入临时文件夹中,然后在move到目标位置

[lastLine   writeToFile: fullPath
            atomically: NO
            encoding: NSASCIIStringEncoding
            error: &error];

NSError* error = nil;//先赋一个nil的值
[lastLine writeToFile:fullPath atomically:NO encoding:NSASCIIStringEncoding error:&error];//注意这里使用的内存地址
if ( error != nil ) {
    [NSApp presentError:error];
}

------------------------
读取文件内容(可以获取文件的编码)
NSString* fileName = @"GrecianUrn.txt";
NSString* homeDir = NSHomeDirectory();
NSString* fullPath = [homeDir stringByAppendingPathComponent:fileName];
NSError* error = nil;
NSStringEncoding encoding;//注意这里的编码
NSString* contents = [NSString stringWithContentsOfFile: fullPath
usedEncoding: &encoding//引用
error: &error];
if ( error != nil ) {
    [NSApp presentError:error];
}

你可能感兴趣的:(ios,c,unix,xcode,Safari)