Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -5,7 +5,7 @@ Given a string and defined variables or values, [[wp:String literal#Variable_int
|
|||
|
||||
:(Languages usually include an infrequently used character or sequence of characters to indicate what is to be replaced such as "%", or "#" rather than "X").
|
||||
|
||||
The task is to:
|
||||
;The task is to:
|
||||
# Use your languages inbuilt string interpolation abilities to interpolate a string missing the text <code>"little"</code> which is held in a variable, to produce the output string <code>"Mary had a little lamb"</code>.
|
||||
# If possible, give links to further documentation on your languages string interpolation features.
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
#!/usr/bin/awk -f
|
||||
BEGIN {
|
||||
str="Mary had a # lamb; # and blue.";
|
||||
gsub(/#/,"big",str);
|
||||
print str;
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
with Ada.Strings.Fixed, Ada.Text_IO;
|
||||
use Ada.Strings, Ada.Text_IO;
|
||||
procedure String_Replace is
|
||||
Original : constant String := "Mary had a @__@ lamb.";
|
||||
Tbr : constant String := "@__@";
|
||||
New_Str : constant String := "little";
|
||||
Index : Natural := Fixed.Index (Original, Tbr);
|
||||
begin
|
||||
Put_Line (Fixed.Replace_Slice (
|
||||
Original, Index, Index + Tbr'Length - 1, New_Str));
|
||||
end String_Replace;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Put_Line ("Mary had a " & New_Str & " lamb.");
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. interpolation-included.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 extra PIC X(6) VALUE "little".
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY FUNCTION SUBSTITUTE("Mary had a X lamb.", "X", extra)
|
||||
|
||||
GOBACK
|
||||
.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
import strutils
|
||||
|
||||
var str = "little"
|
||||
echo "Mary had a $# lamb" % [str]
|
||||
# doesn't need an array for one substitution, but use an array for multiple substitutions
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
*process or(!) source xref attributes;
|
||||
sit: Proc Options(main);
|
||||
/*********************************************************************
|
||||
* Test string replacement
|
||||
* 02.08.2013 Walter Pachl
|
||||
*********************************************************************/
|
||||
Dcl s Char(50) Var Init('Mary had a &X lamb. It is &X');
|
||||
Put Edit(repl(s,'little','&X'))(Skip,A);
|
||||
|
||||
repl: Proc(str,new,old) Returns(Char(50) Var);
|
||||
/*********************************************************************
|
||||
* ooREXX has CHANGESTR(old,str,new[,count])
|
||||
* repl follows, however, the translate "philosophy"
|
||||
* translate(str,new,old) when old and new are just a character each
|
||||
* and replaces all occurrences of old in str by new
|
||||
*********************************************************************/
|
||||
Dcl str Char(*) Var;
|
||||
Dcl (new,old) Char(*);
|
||||
Dcl (res,tmp) Char(50) Var init('');
|
||||
Dcl p Bin Fixed(31);
|
||||
tmp=str; /* copy the input string */
|
||||
Do Until(p=0);
|
||||
p=index(tmp,old); /* position of old in tmp */
|
||||
If p>0 Then Do; /* found */
|
||||
res=res!!left(tmp,p-1)!!new; /* append new to current result*/
|
||||
tmp=substr(tmp,p+length(old)); /* prepare rest of input */
|
||||
End;
|
||||
End;
|
||||
res=res!!tmp; /* final append */
|
||||
Return(res);
|
||||
End;
|
||||
End;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$extra = "little"
|
||||
"Mary had a {0} lamb." -f $extra
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$extra = "little"
|
||||
[console]::writeline("Mary had a {0} lamb.", $extra)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
$extra = "little"
|
||||
[string]::Format("Mary had a {0} lamb.", $extra)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Extra = little,
|
||||
format('Mary had a ~w lamb.', [Extra]), % display result
|
||||
format(atom(Atom), 'Mary had a ~w lamb.', [Extra]). % ... or store it a variable
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Extra = little,
|
||||
Atom = 'Mary had a ~w lamb' $ Extra.
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Extra = little,
|
||||
Atom = 'Mary had a $Extra lamb'.
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
object StringInterpolation extends App {
|
||||
|
||||
import util.matching.Regex._
|
||||
val size = "little"
|
||||
|
||||
{ // Method I (preferred)
|
||||
// Scala 2.10.0 supports direct string interpolation
|
||||
// by putting "s" at the beginning of the string.
|
||||
println("V2.10+ : " + s"Mary had a $size lamb,")
|
||||
}
|
||||
|
||||
{ // Method II
|
||||
// Pre Scala 2.10 indirect use of Java Class Formatter
|
||||
val originalFormatter = "Mary had a %s lamb,"
|
||||
println("V2.10- 1: " + originalFormatter format size)
|
||||
// Above mentioned is Scala's postfix notation and equivalent for:
|
||||
println("V2.10- 2: " + originalFormatter.format(size))
|
||||
// Also possible
|
||||
printf(("V2.10- 3: " + originalFormatter + '\n').format(size))
|
||||
// All will be expanded to
|
||||
print(("V2.10- 3: " + originalFormatter + '\n').format(size))
|
||||
print((new java.util.Formatter).format("V2.10- 4: " + originalFormatter + '\n', size))
|
||||
}
|
||||
|
||||
{ // Method III
|
||||
// Regular expressions, only for demonstration
|
||||
val extractor = """\$\{([^}]+)\}""".r
|
||||
println((extractor.replaceAllIn("Regex 1: Mary had a ${x} lamb,", "snow white")))
|
||||
|
||||
// RegEx freaking
|
||||
def interpolate(text: String, vars: (String, String)*) =
|
||||
extractor.replaceAllIn(text,
|
||||
_ match { case Groups(v) => vars.toMap.getOrElse(v, "" /*in case nothing provided*/ ) })
|
||||
|
||||
println(interpolate("Regex 2: ${who} had a ${size} ${pet}, ${unknown}",
|
||||
("pet", "lamb"), ("size", "fat"), ("size", "humongous"), ("who", "Mary")))
|
||||
}
|
||||
|
||||
{ // Method IV, not recommended.
|
||||
// Standard API method, search argument (1st ones) supposed to be a regular expression
|
||||
println("Replace1: " + "Mary had a ${x} lamb".replaceAll("""\$\{x\}""", size))
|
||||
// Standard API method, literally, on regular expression
|
||||
println("Replace2: " + "Mary had a ${x} lamb".replaceAllLiterally("${x}", size))
|
||||
}
|
||||
|
||||
{ // Method IV, not recommended.
|
||||
println("Split : " + "Mary had a ${x} lamb.".split("""\$\{([^}]+)\}""").mkString(size))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue