Word Replacement
rascal-0.40.17
Synopsis
Replace words in a string.
Description
Suppose you are a book editor and want to ensure that all chapter and section titles are properly capitalized. Here is how to do this.
Examples
import String;
str capitalize(str word:/^<letter:[a-z]><rest:.*>/)
= "<toUpperCase(letter)><rest>";
default str capitalize(str word) = word;
test bool capitalize1() = capitalize("1") == "1";
test bool capitalize2() = capitalize("rascal") == "Rascal";
@synopsis{Version 1: capAll1: using a while loop}
str capAll1(str S) ❷
{
result = "";
while (/^<before:\W*><word:\w+><after:.*$>/ := S) {
result = result + before + capitalize(word);
S = after;
}
return result;
}
test bool tstCapAll1() = capAll1("turn this into a title") == "Turn This Into A Title";
@synopsis{Version 2: capAll2: using visit}
str capAll2(str S) ❸
{
return visit(S){
case /^<word:\w+>/i => capitalize(word)
};
}
test bool tstCapAll2() = capAll2("turn this into a title") == "Turn This Into A Title";
❶ We start by introducing a helper function
capitalize
that does the actual capitalization of a single word. See Regular Pattern for details about regular expression patterns. Next we give two versions of a capitalization function for a sentence:❷
capAll1
uses a while loop to find subsequent words and to replace them by a capitalized version.❸
capAll2
uses a Visit to visit all words in the sentence and replace them by a capitalized version.
Here are some examples:
rascal>capitalize("rascal");
str: "Rascal"
---
Rascal
---
rascal>capAll1("turn this into a capitalized title")
str: "Turn This Into A Capitalized Title"
---
Turn This Into A Capitalized Title
---
rascal>capAll2("turn this into a capitalized title")
str: "Turn This Into A Capitalized Title"
---
Turn This Into A Capitalized Title
---