3

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 clear enough.

Thanks in advance.

Conk
  • 33
  • 1
  • 4

3 Answers3

7

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];
}
Mouna Cheikhna
  • 38,870
  • 10
  • 48
  • 69
2

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)

Community
  • 1
  • 1
phlebotinum
  • 1,362
  • 1
  • 14
  • 17
1

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

Ben Jakuben
  • 3,147
  • 11
  • 62
  • 104
  • What I need to do is to create an application that changes the contents of a file on my computer. I want the process to be automated. – Conk Aug 19 '11 at 12:31
  • 2
    Oops - looks like I need another cup of tea to make me read more carefully. – Ben Jakuben Aug 19 '11 at 12:34