xcode 如何使用 Objective C 查找和替换文件中的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7121532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Find & Replace text in a file using Objective C?
提问by Conk
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.
我需要制作一个能够打开文件并替换其内容的应用程序。
E.g. (in psuedo code)
例如(在伪代码中)
Replace("String1", "String2", "~/Desktop/Sample.txt")
Replace("String1", "String2", "~/Desktop/Sample.txt")
Please let me know if I'm not clear enough.
如果我不够清楚,请告诉我。
Thanks in advance.
提前致谢。
采纳答案by phlebotinum
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). Check the documentation for other types of encoding.
请注意,在此示例中,我使用了 windows 编码(这意味着它在每行末尾使用字符 \n\r)。检查其他类型编码的文档。
(See NSString
documentation)
(见NSString
文档)
回答by Mouna Cheikhna
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];
}
回答by Ben Jakuben
For Xcode 4, open the file you want to search, then click Edit > Find > Find and Replace, or the keyboard shortcut Command + Option + f.
对于 Xcode 4,打开要搜索的文件,然后单击编辑 > 查找 > 查找和替换,或键盘快捷键 Command + Option + f。