Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you append to an already existing string?

People also ask

How do I append to an existing string?

You can use the '+' operator to append two strings to create a new string. There are various ways such as using join, format, string IO, and appending the strings with space.

Can we append in string?

If you simply want to concatenate a string 'n' times, you can do it easily using s = 'Hi' * 10 . Another way to perform string append operation is by creating a list and appending strings to the list. Then use string join() function to merge them together to get the result string.


In classic sh, you have to do something like:

s=test1
s="${s}test2"

(there are lots of variations on that theme, like s="$s""test2")

In bash, you can use +=:

s=test1
s+=test2

$ string="test"
$ string="${string}test2"
$ echo $string
testtest2

#!/bin/bash
message="some text"
message="$message add some more"

echo $message

some text add some more


teststr=$'test1\n'
teststr+=$'test2\n'
echo "$teststr"

VAR=$VAR"$VARTOADD(STRING)"   
echo $VAR

#!/bin/bash

msg1=${1} #First Parameter
msg2=${2} #Second Parameter

concatString=$msg1"$msg2" #Concatenated String
concatString2="$msg1$msg2"

echo $concatString 
echo $concatString2