String concatenation: Difference between revisions

Add Ecstasy example
(Add Ecstasy example)
 
(12 intermediate revisions by 10 users not shown)
Line 773:
b$ = a$ & " world"
print b$</syntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="ecstasy">
module StringAppend {
void run() {
String start = "hello";
String finish = " world";
 
// approach #1: add strings together
String approach1 = start + finish;
 
// approach #2: StringBuffer
String approach2 = new StringBuffer()
.append(start)
.append(finish)
.toString();
 
// approach #3: string template
String approach3 = $"{start}{finish}";
 
@Inject Console console;
console.print($|
|Appending strings:
|
| {start=}
| {finish=}
|
| {approach1=}
| {approach2=}
| {approach3=}
|
);
}
}
</syntaxhighlight>
 
{{out}}
<pre>
x$ xec doc/examples/StringAppend
 
Appending strings:
 
start=hello
finish= world
 
approach1=hello world
approach2=hello world
approach3=hello world
</pre>
 
=={{header|Ela}}==
Line 790 ⟶ 839:
 
=={{header|Elena}}==
ELENA 46.x:
<syntaxhighlight lang="elena">public program()
{
Line 796 ⟶ 845:
var s2 := s + " literal";
console.writeLine:(s);
console.writeLine:(s2);
console.readChar()
}</syntaxhighlight>
Line 804 ⟶ 853:
Hello
Hello literal
</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
text s = "hello"
write(s)
writeLine(" literal")
text s2 = s + " literal"
writeLine(s2)
</syntaxhighlight>
{{out}}
<pre>
hello literal
hello literal
</pre>
 
Line 924 ⟶ 987:
fansh> b
abcdef</syntaxhighlight>
 
<syntaxhighlight lang="java">
/* gary chike 08/27/2023 */
 
class Main
{
static Void main() {
s1 := "Only The"
s2 := "knows"
s3 := s1 + " Fantom " + s2 + "!" // Concatenation
echo(s3)
s4 := "$s1 Fantom $s2!" // String interpolation
echo(s4)
}
}
</syntaxhighlight>
 
{{out}}
<pre>
Only The Fantom knows!
Only The Fantom knows!
</pre>
 
=={{header|Fe}}==
'''pack''' is not a built-in function, see its definition [[Reverse_a_string#Fe|here]].
<syntaxhighlight lang="clojure">
(print (pack '("Hello" " world!")))
</syntaxhighlight>
 
=={{header|Forth}}==
Line 993 ⟶ 1,084:
 
End</syntaxhighlight>
 
=={{header|GDScript}}==
{{works with|Godot|4.0.1}}
 
<syntaxhighlight lang="gdscript">
extends MainLoop
 
 
func _process(_delta: float) -> bool:
var first: String = "123"
var second: String = first + "abc"
 
print(first)
print(second)
 
return true # Exit
</syntaxhighlight>
 
=={{header|GlovePIE}}==
Line 1,085 ⟶ 1,193:
 
=={{header|Java}}==
There are multiple ways to concatenate string values in Java.<br />
The most common way is through the plus operator.
<syntaxhighlight lang="java">
String string = "abc" + "def";
</syntaxhighlight>
Which can also be written as
<syntaxhighlight lang="java">
String string = "abc";
string += "def";
</syntaxhighlight>
There is also the ''String.concat'' method
<syntaxhighlight lang="java">
String string = "abc".concat("def");
</syntaxhighlight>
You could use a ''StringBuilder'' object if you're appending multiple strings.
<syntaxhighlight lang="java">
StringBuilder string = new StringBuilder();
string.append("abc").append("def");
</syntaxhighlight>
''StringBuilder'' also conveniently lets you insert strings within strings.<br />
So, you can also concatenate a string as follows
<syntaxhighlight lang="java">
StringBuilder string = new StringBuilder();
string.append("abc");
string.insert(3, "def");
</syntaxhighlight>
A less common approach would be to use the ''String.format'' or ''String.formatted'' methods.
<syntaxhighlight lang="java">
String string = String.format("%s%s", "abc", "def");
</syntaxhighlight>
<syntaxhighlight lang="java">
String string = "%s%s".formatted("abc", "def");
</syntaxhighlight>
All of these methods will produce the following string
<pre>
abcdef
</pre>
<br />
Alternately
<syntaxhighlight lang="java5">public class Str{
public static void main(String[] args){
Line 1,100 ⟶ 1,247:
<syntaxhighlight lang="javascript">var s = "hello"
print(s + " there!")</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">
"title:" " text" concat.</syntaxhighlight>
{{out}}
<pre>"title: text"</pre>
 
=={{header|jq}}==
Line 1,157 ⟶ 1,310:
{christian_name} {name}
-> Albert de Jeumont-Schneidre
</syntaxhighlight>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
$s1 = hello
$s2 = \sworld
 
fn.println($s1 world)
# Output: hello world
 
fn.println($s1$s2)
# Output: hello world
 
fn.println(fn.concat($s1, $s2))
# Output: hello world
 
fn.println(parser.op($s1 ||| $s2))
# Output: hello world
 
fn.println(fn.add($s1, $s2))
# Output: hello world
 
fn.println(parser.op($s1 + $s2))
# Output: hello world
</syntaxhighlight>
 
Line 1,789 ⟶ 1,966:
 
=={{header|PureBasic}}==
<syntaxhighlight lang="purebasicbasic">If OpenConsole()
If OpenConsole()
 
s$ = "hello"
PrintN( s$ + " literal")
Line 1,800 ⟶ 1,977:
CloseConsole()
EndIf</syntaxhighlight>
 
{{out}}
<pre>
hello literal
hello literal
</pre>
 
This version uses the debugger versus outputting to the console. It
implements 'EnableExplicit' which is similar to VisuaBasic's 'Option Explicit' which means all variables must be declared. It also features the use of string variables WITHOUT the dollar-sign suffix '$' which is common in BASIC variants to indicate the string datatype:
<syntaxhighlight lang="basic">
EnableExplicit
 
Define.s s1, s2, s3
 
s1 = "Hello "
s2 = "World"
s3 = s1 + s2
Debug s3
s3 = s3 + "!"
Debug s3
</syntaxhighlight>
 
{{out}}
<pre>
Hello World
Hello World!
</pre>
 
=={{header|Python}}==
Line 2,073 ⟶ 2,277:
-->s2
Hello world! </pre>
 
=={{header|sed}}==
There are no variables in ''sed'', just two distinct locations for storing a string: The "pattern space" and the "hold space".
 
The pattern space contains the current input line. With the <code>h</code> command, it is copied to the hold space. Then, the <code>s</code> command appends a string literal to the pattern space:
<syntaxhighlight lang="sed">h
s/$/String Literal/</syntaxhighlight>
If necessary, the content of both spaces could be exchanged by a final <code>x</code> command.
 
=={{header|Seed7}}==
Line 2,302 ⟶ 2,514:
# outputs "hello world"
out s2 endl console</syntaxhighlight>
 
=={{header|Uxntal}}==
<syntaxhighlight lang="Uxntal">|10 @Console &vector $2 &read $1 &pad $4 &type $1 &write $1 &error $1
 
|0100 @on-reset ( -> )
;str3 ;str1 copy-str
;str3 ;str2 append-str
;str3 print-str
#0a .Console/write DEO
BRK
 
@print-str ( str* -: )
&loop ( -- )
LDAk .Console/write DEO
INC2 LDAk ?&loop
POP2 JMP2r
 
@copy-str ( dest* src* -: )
STH2
&loop ( -- )
LDAkr STH2k STAr INC2 LDAkr STHr INC2r ?&loop
POP2 POP2r JMP2r
 
@append-str ( dest* src* -: )
STH2 end-str STH2r copy-str JMP2r
 
@end-str ( str* -: str* )
!&inner
&loop ( -- )
INC2 &inner LDAk ?&loop
JMP2r
 
@str1 "Uxn 00
@str2 "tal 00
@str3</syntaxhighlight>
 
=={{header|Vala}}==
Line 2,378 ⟶ 2,625:
 
=={{header|Wren}}==
<syntaxhighlight lang="javascriptwren">var s = "Hello, "
var t = s + "world!"
System.print(s)
162

edits