all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
12
Task/String-interpolation--included-/0DESCRIPTION
Normal file
12
Task/String-interpolation--included-/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{{basic data operation}}
|
||||
{{omit from|NSIS}}{{omit from|BBC BASIC}}
|
||||
Given a string and defined variables or values, [[wp:String literal#Variable_interpolation|string interpolation]] is the replacement of defined character sequences in the string by values or variable values.
|
||||
: For example, given an original string of <code>"Mary had a X lamb."</code>, a value of "big", and if the language replaces X in its interpolation routine, then the result of its interpolation would be the string <code>"Mary had a big lamb"</code>.
|
||||
|
||||
:(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:
|
||||
# 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.
|
||||
|
||||
<small>Note: The task is not to create a string interpolation routine, but to show a language's built-in capability.</small>
|
||||
4
Task/String-interpolation--included-/1META.yaml
Normal file
4
Task/String-interpolation--included-/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- String manipulation
|
||||
note: Basic language learning
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
main:(
|
||||
# as a STRING #
|
||||
STRING extra = "little";
|
||||
printf(($"Mary had a "g" lamb."l$, extra));
|
||||
|
||||
# as a FORMAT #
|
||||
FORMAT extraf = $"little"$;
|
||||
printf($"Mary had a "f(extraf)" lamb."l$);
|
||||
|
||||
# or: use simply use STRING concatenation #
|
||||
print(("Mary had a "+extra+" lamb.", new line))
|
||||
)
|
||||
|
|
@ -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,5 @@
|
|||
const little = "little"
|
||||
printf ("Mary had a %s lamb\n", little)
|
||||
|
||||
// alternatively
|
||||
println ("Mary had a " + little + " lamb")
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
; Using the = operator
|
||||
LIT = little
|
||||
string = Mary had a %LIT% lamb.
|
||||
|
||||
; Using the := operator
|
||||
LIT := "little"
|
||||
string := "Mary had a" LIT " lamb."
|
||||
|
||||
MsgBox %string%
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
call :interpolate %1 %2 res
|
||||
echo %res%
|
||||
goto :eof
|
||||
|
||||
:interpolate
|
||||
set pat=%~1
|
||||
set str=%~2
|
||||
set %3=!pat:X=%str%!
|
||||
goto :eof
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
>interpolate.cmd "Mary had a X lamb" little
|
||||
Mary had a little lamb
|
||||
|
|
@ -0,0 +1 @@
|
|||
@("Mary had a X lamb":?a X ?z) & str$(!a little !z)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
int main( ) {
|
||||
std::string original( "Mary had a X lamb." ) , toBeReplaced( "X" ) ,
|
||||
replacement ( "little" ) ;
|
||||
std::string newString = original.replace( original.find( "X" ) ,
|
||||
toBeReplaced.length( ) , replacement ) ;
|
||||
std::cout << "String after replacement: " << newString << " \n" ;
|
||||
return 0 ;
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
const char *extra = "little";
|
||||
printf("Mary had a %s lamb.\n", extra);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let [little "little"]
|
||||
(println (format "Mary had a %s lamb." little)))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
size = 'little'
|
||||
console.log "Mary had a #{size} lamb." # Mary had a little lamb.
|
||||
console.log "Escaping: \#{}" # Escaping: #{}
|
||||
console.log 'No #{ interpolation} with single quotes' # No #{ interpolation} with single quotes
|
||||
|
||||
# Multi-line strings and arbtrary expressions work: 20
|
||||
console.log """
|
||||
Multi-line strings and arbtrary expressions work: #{ 5 * 4 }
|
||||
"""
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let ((extra "little"))
|
||||
(format t "Mary had a ~A lamb.~%" extra))
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import std.stdio: writeln;
|
||||
import std.string: format;
|
||||
|
||||
void main() {
|
||||
auto original = "Mary had a %s lamb.";
|
||||
auto extra = "little";
|
||||
auto modified = format(original, extra);
|
||||
writeln(modified);
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
PrintLn(Format('Mary had a %s lamb.', ['little']))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def adjective := "little"
|
||||
`Mary had a $adjective lamb`
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def adjective := "little"
|
||||
simple__quasiParser.valueMaker("Mary had a ${0} lamb").substitute([adjective])
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
constant lambType = "little"
|
||||
sequence s
|
||||
s = sprintf("Mary had a %s lamb.",{lambType})
|
||||
puts(1,s)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
USE: formatting
|
||||
|
||||
SYMBOL: little
|
||||
|
||||
"little" little set
|
||||
|
||||
little get "Mary had a %s lamb" sprintf
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
USE: formatting
|
||||
|
||||
CONSTANT: little "little"
|
||||
|
||||
little "Mary had a %s lamb" sprintf
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
fansh> x := "little"
|
||||
little
|
||||
fansh> echo ("Mary had a $x lamb")
|
||||
Mary had a little lamb
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
program interpolate
|
||||
|
||||
write (*,*) trim(inter("Mary had a X lamb.","X","little"))
|
||||
|
||||
contains
|
||||
|
||||
elemental function inter(string,place,ins) result(new)
|
||||
character(len=*), intent(in) :: string,place,ins
|
||||
character(len=len(string)+max(0,len(ins)-len(place))) :: new
|
||||
integer :: idx
|
||||
idx = index(string,place)
|
||||
if ( idx == 0 ) then
|
||||
new = string
|
||||
else
|
||||
new = string(1:idx-1)//ins//string(idx+len(place):len(string))
|
||||
end if
|
||||
end function inter
|
||||
|
||||
end program interpolate
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
str := "Mary had a %s lamb"
|
||||
txt := "little"
|
||||
out := fmt.Sprintf(str, txt)
|
||||
fmt.Println(out)
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def adj = 'little'
|
||||
assert 'Mary had a little lamb.' == "Mary had a ${adj} lamb."
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import Text.Printf
|
||||
|
||||
main = printf "Mary had a %s lamb\n" "little"
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
CHARACTER original="Mary had a X lamb", little = "little", output_string*100
|
||||
|
||||
output_string = original
|
||||
EDIT(Text=output_string, Right='X', RePLaceby=little)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
s2 := "humongous"
|
||||
s3 := "little"
|
||||
s1 := "Mary had a humongous lamb."
|
||||
s1 ?:= tab(find(s2)) || (=s2,s3) || tab(0) # replaces the first instance of s2 with s3
|
||||
while s1 ?:= tab(find(s2)) || (=s2,s3) || tab(0) # replaces all instances of s2 with s3, equivalent to replace
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
require 'printf'
|
||||
'Mary had a %s lamb.' sprintf <'little'
|
||||
Mary had a little lamb.
|
||||
|
||||
require 'strings'
|
||||
('%s';'little') stringreplace 'Mary had a %s lamb.'
|
||||
Mary had a little lamb.
|
||||
'Mary had a %s lamb.' rplc '%s';'little'
|
||||
Mary had a little lamb.
|
||||
|
|
@ -0,0 +1 @@
|
|||
open'strings printf'
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
String original = "Mary had a X lamb";
|
||||
String little = "little";
|
||||
String replaced = original.replace("X", little); //does not change the original String
|
||||
System.out.println(replaced);
|
||||
//Alternative:
|
||||
System.out.printf("Mary had a %s lamb.", little);
|
||||
//Alternative:
|
||||
String formatted = String.format("Mary had a %s lamb.", little);
|
||||
System.out.println(formatted);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var original = "Mary had a X lamb";
|
||||
var little = "little";
|
||||
var replaced = original.replace("X", little); //does not change the original string
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
str = string.gsub( "Mary had a X lamb.", "X", "little" )
|
||||
print( str )
|
||||
|
|
@ -0,0 +1 @@
|
|||
print "Mary had a \n lamb" -- The \n is interpreted as an escape sequence for a newline
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Extra = "little";
|
||||
StringReplace["Mary had a X lamb.", {"X" -> Extra}]
|
||||
->"Mary had a little lamb."
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf(true, "Mary had a ~a lamb", "little");
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.IO; // contains printf() and print()
|
||||
|
||||
module Stringy
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def extra = "little";
|
||||
printf("Mary had a %s lamb.\n", extra);
|
||||
print("Mary had a $extra lamb.\n");
|
||||
WriteLine($"Mary had a $extra lamb.");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/* NetRexx */
|
||||
|
||||
options replace format comments java crossref savelog symbols
|
||||
|
||||
import java.text.MessageFormat
|
||||
import java.text.FieldPosition
|
||||
|
||||
useBif()
|
||||
useMessageFormat()
|
||||
|
||||
return
|
||||
|
||||
method useBif public static
|
||||
|
||||
st = "Mary had a %1$ lamb."
|
||||
si = 'little'
|
||||
|
||||
say st.changestr('%1$', si)
|
||||
|
||||
return
|
||||
|
||||
method useMessageFormat public static
|
||||
|
||||
result = StringBuffer('')
|
||||
|
||||
args = Object [ -
|
||||
Object Integer(7), -
|
||||
Object Date(), -
|
||||
Object 'a disturbance in the Force' -
|
||||
]
|
||||
msgfmt = MessageFormat('At {1, time} on {1, date}, there was {2} on planet {0, number, integer}.')
|
||||
result = msgfmt.format(args, result, FieldPosition(0))
|
||||
say result
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let extra = "little" in
|
||||
Printf.printf "Mary had a %s lamb." extra
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
declare
|
||||
X = "little"
|
||||
in
|
||||
{System.showInfo "Mary had a "#X#" lamb"}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
GEN
|
||||
string_interpolate(GEN n)
|
||||
{
|
||||
pari_printf("The value was: %Ps.\n", n);
|
||||
GEN s = pari_sprintf("Storing %Ps in a string", n);
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
s=Strprintf("The value was: %Ps", 1<<20);
|
||||
printf("The value was: %Ps", 1<<20);
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
$extra = 'little';
|
||||
echo "Mary had a $extra lamb.\n";
|
||||
printf("Mary had a %s lamb.\n", $extra);
|
||||
?>
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
my $extra = "little";
|
||||
say "Mary had a $extra lamb"; # variable interpolation
|
||||
say "Mary had a { $extra } lamb"; # expression interpolation
|
||||
printf "Mary had a %s lamb.\n", $extra; # standard printf
|
||||
say $extra.fmt("Mary had a %s lamb"); # inside-out printf
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$extra = "little";
|
||||
print "Mary had a $extra lamb.\n";
|
||||
printf "Mary had a %s lamb.\n", $extra;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(let Extra "little"
|
||||
(prinl (text "Mary had a @1 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 @@
|
|||
ReplaceString("Mary had a X lamb.","X","little")
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
; String variable can be defined by appending .s to its name during definition or by appending and using $ as a part of its name.
|
||||
Define txt$, txtvar.s="little"
|
||||
|
||||
;Load datasegment into variable txt$
|
||||
Restore Mary
|
||||
Read.s txt$
|
||||
|
||||
; Replace X with "little" and store result in txt$
|
||||
txt$=ReplaceString(txt$,"X",txtvar)
|
||||
|
||||
OpenConsole(): Print(txt$)
|
||||
|
||||
DataSection:
|
||||
Mary:
|
||||
Data.s "Mary had a X lamb."
|
||||
EndDataSection
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
>>> original = 'Mary had a %s lamb.'
|
||||
>>> extra = 'little'
|
||||
>>> original % extra
|
||||
'Mary had a little lamb.'
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
>>> original = 'Mary had a {extra} lamb.'
|
||||
>>> extra = 'little'
|
||||
>>> original.format(**locals())
|
||||
'Mary had a little lamb.'
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
>>> original = 'Mary had a {0} lamb.'
|
||||
>>> extra = 'little'
|
||||
>>> original.format(extra)
|
||||
'Mary had a little lamb.'
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
>>> from string import Template
|
||||
>>> original = Template('Mary had a $extra lamb.')
|
||||
>>> extra = 'little'
|
||||
>>> original.substitute(**locals())
|
||||
'Mary had a little lamb.'
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
str: "Mary had a <%size%> lamb"
|
||||
size: "little"
|
||||
build-markup str
|
||||
|
||||
;REBOL3 also has the REWORD function
|
||||
str: "Mary had a $size lamb"
|
||||
reword str [size "little"]
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*REXX program to demonstrate string interpolation (string replacement).*/
|
||||
/*the string to be replaced is */
|
||||
replace = "little" /*usually a unique character(s) */
|
||||
/*string and is case sensative.*/
|
||||
original1 = "Mary had a X lamb."
|
||||
new1 = changestr('X', original1, replace)
|
||||
say 'original1 =' original1
|
||||
say 'replaced =' new1
|
||||
say
|
||||
|
||||
original2 = "Mary had a % lamb."
|
||||
new2 = changestr('%', original2, replace)
|
||||
say 'original2 =' original2
|
||||
say 'replaced =' new2
|
||||
say
|
||||
|
||||
original3 = "Mary had a $$$ lamb."
|
||||
new3 = changestr('$$$',original3,replace)
|
||||
say 'original3 =' original3
|
||||
say 'replaced3 =' new3
|
||||
say
|
||||
|
||||
original4 = "Mary had a someKindOf lamb."
|
||||
new3 = changestr('someKindOf', original4, "little")
|
||||
say 'original4 =' original4
|
||||
say 'replaced4 =' new3
|
||||
/*stick a fork in it, we're done.*/
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#lang racket
|
||||
|
||||
(format "Mary had a ~a lamb" "little")
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
irb(main):001:0> extra = 'little'
|
||||
=> "little"
|
||||
irb(main):002:0> "Mary had a #{extra} lamb."
|
||||
=> "Mary had a little lamb."
|
||||
irb(main):003:0> "Mary had a %s lamb." % extra
|
||||
=> "Mary had a little lamb."
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
a$ = Mary had a X lamb."
|
||||
a$ = word$(a$,1,"X")+"little"+word$(a$,2,"X")
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
s1 = "Mary had a humongous lamb."
|
||||
s2 = "humongous"
|
||||
s3 = "little"
|
||||
s1 s2 = s3
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
val extra = "little"
|
||||
val result = s"Mary had a $extra lamb."
|
||||
|
|
@ -0,0 +1 @@
|
|||
Mary had a little lamb.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
val original = "Mary had a %s lamb."
|
||||
val extra = "little"
|
||||
original format extra
|
||||
|
|
@ -0,0 +1 @@
|
|||
Mary had a little lamb.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
# Usage example: . interpolate "Mary has a X lamb" "quite annoying"
|
||||
echo "$1" | sed "s/ X / $2 /g"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
const string: original is "Mary had a X lamb";
|
||||
const string: little is "little";
|
||||
var string: replaced is "";
|
||||
begin
|
||||
replaced := replace(original, "X", little);
|
||||
writeln(replaced);
|
||||
end func;
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
$$ MODE TUSCRIPT
|
||||
|
||||
sentence_old="Mary had a X lamb."
|
||||
|
||||
values=*
|
||||
DATA little
|
||||
DATA big
|
||||
|
||||
sentence_new=SUBSTITUTE (sentence_old,":X:",0,0,values)
|
||||
PRINT sentence_old
|
||||
PRINT sentence_new
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
set size "little"
|
||||
puts "Mary had a $size lamb."
|
||||
|
||||
proc RandomWord {args} {
|
||||
lindex $args [expr {int(rand()*[llength $args])}]
|
||||
}
|
||||
puts "Mary had a [RandomWord little big] lamb."
|
||||
|
|
@ -0,0 +1 @@
|
|||
puts [format "Mary had a %s %s." [RandomWord little big] [RandomWord lamb piglet calf]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
set s "Mary had a @SIZE@ lamb."
|
||||
puts [string map {@SIZE@ "miniscule"} $s]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
set grandpa {$daddy}; set grandma \$mommy
|
||||
set daddy myself; set mommy {lil' bro}
|
||||
set fun1 \[string\ to
|
||||
set fun2 lower
|
||||
set lower middle
|
||||
set middle upper
|
||||
set fun3 {aNd]}
|
||||
puts [subst "$grandpa $fun1$[subst $$fun2] $fun3 $grandma"]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
extra='little'
|
||||
echo Mary had a $extra lamb.
|
||||
echo "Mary had a $extra lamb."
|
||||
printf "Mary had a %s lamb.\n" $extra
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
set extra='little'
|
||||
echo Mary had a $extra lamb.
|
||||
echo "Mary had a $extra lamb."
|
||||
printf "Mary had a %s lamb.\n" $extra
|
||||
|
|
@ -0,0 +1 @@
|
|||
-[foo-[ x ]-bar]-
|
||||
|
|
@ -0,0 +1 @@
|
|||
-[foo-[. f ]-bar]-
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
x = <'little'>
|
||||
|
||||
#show+
|
||||
|
||||
main = -[Mary had a -[. ~& ]- lamb.]- x
|
||||
Loading…
Add table
Add a link
Reference in a new issue