How to Concatenate a String with a Variable

How to concatenate string variables in Bash

foo="Hello"
foo="${foo} World"
echo "${foo}"
> Hello World

In general to concatenate two variables you can just write them one after another:

a='Hello'
b='World'
c="${a} ${b}"
echo "${c}"
> Hello World

How do I concatenate a string with a variable?

Your code is correct. Perhaps your problem is that you are not passing an ID to the AddBorder function, or that an element with that ID does not exist. Or you might be running your function before the element in question is accessible through the browser's DOM.

Since ECMAScript 2015, you can also use template literals (aka template strings):

document.getElementById(`horseThumb_${id}`).className = "hand positionLeft";

To identify the first case or determine the cause of the second case, add these as the first lines inside the function:

alert('ID number: ' + id);
alert('Return value of gEBI: ' + document.getElementById('horseThumb_' + id));

That will open pop-up windows each time the function is called, with the value of id and the return value of document.getElementById. If you get undefined for the ID number pop-up, you are not passing an argument to the function. If the ID does not exist, you would get your (incorrect?) ID number in the first pop-up but get null in the second.

The third case would happen if your web page looks like this, trying to run AddBorder while the page is still loading:

<head>
<title>My Web Page</title>
<script>
function AddBorder(id) {
...
}
AddBorder(42); // Won't work; the page hasn't completely loaded yet!
</script>
</head>

To fix this, put all the code that uses AddBorder inside an onload event handler:

// Can only have one of these per page
window.onload = function() {
...
AddBorder(42);
...
}

// Or can have any number of these on a page
function doWhatever() {
...
AddBorder(42);
...
}

if(window.addEventListener) window.addEventListener('load', doWhatever, false);
else window.attachEvent('onload', doWhatever);

How to concatenate a fixed string and a variable in Python

I'm guessing that you meant to do this:

msg['Subject'] = "Auto Hella Restart Report " + sys.argv[1]
# To concatenate strings in Python, use ^

How to concat variable and string in bash script

Strings are concatenated by default in the shell.

value="$variable"text"$other_variable"

It's generally considered good practice to wrap variable expansions in double quotes.

You can also do this:

value="${variable}text${other_variable}"

The curly braces are useful when dealing with a mixture of variable names and strings.

Note that there should be no spaces around the = in an assignment.

JavaScript - Concatenate a value within variable

I would consider template literals - also called backticks

cy.get(`[data-value="${value}"]`)

Concatenate strings and variable values

You could try to convert today.year into a string using str().

It would be something like that:

"In year " + str(today.year) + " I should learn more Python"

How do I concatenate strings and variables in PowerShell?

Write-Host "$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)"

See the Windows PowerShell Language Specification Version 3.0, p34, sub-expressions expansion.

C concatenate string variable string

If the name is already decided at compile time and if there is no necessity to change the name during runtime then by all means choose the simplest alternative, i.e. -

#define NAME "Lannister"

char write_letter[] = "Here you are Mrs. " NAME " Welcome!\r\n"
"Getting Started\r\n"
"Interviews\r\n"
"Take-home Projects\r\n";

Compile with highest warning level set. When you do that you will
get a warning similar to "initializer-string for array of chars is
too long
" (this is the warning generated by GCC). 50 is too small for
this array hence I have allowed the compiler to decide the array size
(i.e. 'write_letter[]').

If you need to change the string at runtime then use either strcat() -

char write_letter[150] = "Here you are Mrs. ";
char *name = "Lannister";
char *write_letter_post = " Welcome!\r\n"
"Getting Started\r\n"
"Interviews\r\n"
"Take-home Projects\r\n";

strcat(write_letter, name);
strcat(write_letter, write_letter_post);
/*Use strncat() to prevent buffer overflow possibilities.*/

or, sprintf() -

char *_write_letter = "Here you are Mrs. %s Welcome!\r\n"
"Getting Started\r\n"
"Interviews\r\n"
"Take-home Projects\r\n";
char *name = "Lannister";
char write_letter[150];

sprintf(write_letter, _write_letter, name);
/*Use snprintf() to prevent buffer overflow possibilities.*/

Concatenating variables and strings in React

You're almost correct, just misplaced a few quotes. Wrapping the whole thing in regular quotes will literally give you the string #demo + {this.state.id} - you need to indicate which are variables and which are string literals. Since anything inside {} is an inline JSX expression, you can do:

href={"#demo" + this.state.id}

This will use the string literal #demo and concatenate it to the value of this.state.id. This can then be applied to all strings. Consider this:

var text = "world";

And this:

{"Hello " + text + " Andrew"}

This will yield:

Hello world Andrew 

You can also use ES6 string interpolation/template literals with ` (backticks) and ${expr} (interpolated expression), which is closer to what you seem to be trying to do:

href={`#demo${this.state.id}`}

This will basically substitute the value of this.state.id, concatenating it to #demo. It is equivalent to doing: "#demo" + this.state.id.



Related Topics



Leave a reply



Submit