How to resolve the algorithm Globally replace text in several files step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Globally replace text in several files step by step in the Wren programming language

Table of Contents

Problem Statement

Replace every occurring instance of a piece of text in a group of text files with another one.

For this task we want to replace the text   "Goodbye London!"   with   "Hello New York!"   for a list of files.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Globally replace text in several files step by step in the Wren programming language

Source code in the wren programming language

import "io" for File

var files = ["file1.txt", "file2.txt"]
for (file in files) {
    var text = File.read(file)
    System.print("%(file) contains: %(text)")
    text = text.replace("Goodbye London!", "Hello New York!")
    File.create(file) { |f|  // overwrites existing file
        f.writeBytes(text)
    }
    System.print("%(file) now contains: %(File.read(file))")
}


  

You may also check:How to resolve the algorithm Bitmap step by step in the Crystal programming language
You may also check:How to resolve the algorithm Combinations with repetitions step by step in the Sidef programming language
You may also check:How to resolve the algorithm Rep-string step by step in the Action! programming language
You may also check:How to resolve the algorithm Isograms and heterograms step by step in the Perl programming language
You may also check:How to resolve the algorithm Numbers with equal rises and falls step by step in the BASIC programming language