[英]How to Find & Replace text in a file using Objective C?
I am new to Xcode and was wondering if anyone could help me with this. 我是Xcode的新手,想知道是否有人可以帮助我。
I need to make an application that is able to open a file and replace its contents. 我需要创建一个能够打开文件并替换其内容的应用程序。
Eg (in psuedo code) 例如(在伪代码中)
Replace("String1", "String2", "~/Desktop/Sample.txt") 替换(“String1”,“String2”,“〜/ Desktop / Sample.txt”)
Please let me know if I'm not clear enough. 如果我不够清楚,请告诉我。
Thanks in advance. 提前致谢。
use stringByReplacingOccurrencesOfString:withString:
method which will find all occurrences of one NSString and replace them, returning a new autoreleased NSString. 使用stringByReplacingOccurrencesOfString:withString:
方法,它将查找所有出现的一个NSString并替换它们,返回一个新的自动释放的NSString。
NSString *source = @"The rain in Spain";
NSString *copy = [source stringByReplacingOccurrencesOfString:@"ain"
withString:@"oof"];
NSLog(@"copy = %@", copy);
// prints "copy = The roof in Spoof"
Edit 编辑
to set the file content in your string (be careful , this is not conveniant if your file is a bit large) , replace occurences then copy to a new file : 在你的字符串中设置文件内容(小心,如果你的文件有点大,这是不方便的),替换出现然后复制到一个新文件:
// Instantiate an NSString which describes the filesystem location of
// the file we will be reading.
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Sample.txt"];
NSError *anError;
NSString *aString = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&anError];
// If the file read was unsuccessful, display the error description.
// Otherwise, copy the string to your file.
if (!aString) {
NSLog(@"%@", [anError localizedDescription]);
} else {
//replace string1 occurences by string2
NSString *replacedString = [aString stringByReplacingOccurrencesOfString:@"String1"
withString:@"String2"];
//copy replacedString to sample.txt
NSString * stringFilepath = @"ReplacedSample.txt";
[replacedString writeToFile:stringFilepath atomically:YES encoding:NSWindowsCP1250StringEncoding error:error];
}
You probably want this 你可能想要这个
And regarding your question about how to read the text from a file to a NSString: 关于如何从文件中读取文本到NSString的问题:
NSError * error;
NSString * stringFromFile;
NSString * stringFilepath = @"loadfile.txt";
stringFromFile = [[NSString alloc] initWithContentsOfFile:stringFilepath
encoding:NSWindowsCP1250StringEncoding
error:&error];
And for writing to a file: (using the same NSString from loading: stringFromFile) 并写入文件:(使用相同的NSString加载:stringFromFile)
NSError * error;
NSString * stringFilepath = @"savefile.txt";
[stringFromFile writeToFile:stringFilepath atomically:YES encoding:NSWindowsCP1250StringEncoding error:error];
Note that in this example i use an encoding for windows (this means it uses charcters \\n\\r at the end of each line). 请注意,在此示例中,我使用Windows编码(这意味着它在每行末尾使用字符\\ n \\ r)。 Check the documentation for other types of encoding. 检查文档以了解其他类型的编码。
对于Xcode 4,打开要搜索的文件,然后单击编辑>查找>查找和替换,或单击键盘快捷键Command + Option + f。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.