开发者

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.

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")

Please let me know if I'm not cle开发者_如何学编程ar enough.

Thanks in advance.


use stringByReplacingOccurrencesOfString:withString: method which will find all occurrences of one NSString and replace them, returning a new autoreleased 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:

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)

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.

(See NSString documentation)


For Xcode 4, open the file you want to search, then click Edit > Find > Find and Replace, or the keyboard shortcut Command + Option + f.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜