Javatpoint Logo
Javatpoint Logo

Concatenate Meaning

In formal English, the term Concatenate is defined as an operation to join or link something together. The same concept is taken to define the operation of concatenation in computer programming. Computer programmers commonly use concatenation for joining end-to-end character strings.

For example, the concatenation of "Hello" + "World" will fetch you the output as "HelloWorld". In many programming languages, string concatenation has its own pre-defined function such as concat(), join(), etc., or the coders can concatenate two or more strings in the program using the '+' (plus) operator.

Concatenate Meaning

Note: If you are concatenating a variable of non-string type with a string, you first need to cast that variable into a string.

In this tutorial we will discover how Concatenation is done in different programming languages with the help of examples.

Concatenate in C

1. Using Loops

The first method to concatenate two strings in C is by loops.

Program

Output:

After concatenation: Concatenate in C programming

Explanation:

In the above example, we have taken two strings and have named them str1 and str2 respectively. Have applied while and for loop to concatenate both the strings and later have stored the result in str1.

NOTE: Please make sure that the length of str1 should be sufficient to store the output after concatenating the string. If the length is short, the C compiler may throw some expected error.

2. Using strcat()

In C programming, there is an inbuilt function strcat(), used to concatenate two string values.

Syntax

Parameter

  • String1: This parameter represents the first string to which the following string will be concatenated.
  • String2: This represents the second string that you wish to add with string1.

Program

Output:

Enter the first number: 34

Enter the second number: 23

The output after concatenation: 3423

Example:

In the above program, we have used the inbuilt strcat() function to concatenate two strings. As you can see, it has taken two strings, i.e., str1 and str2, as its parameters, and then appends the str2 at the end of the str1 (first string).

Concatenate in C++

In C++, you can concatenate two strings together using + (string concatenation) operator or by append() function.

1. By '+' (string concatenation) operator

In C++, the programmers can leverage the advantage of the '+' operator to append two strings together to create a new string. This process is known as concatenation.

Example

Output:

Concatenate Meaning

2. C++ Append() function

A string in C++ is actually an object, which contains functions that can perform certain operations on strings. For example, you can also concatenate strings with the append() function:

Syntax

Parameters

  • String1: This parameter represents the first string to which the following string will be concatenated.
  • String2: This represents the second string that you wish to add with string1.

Example:

Output:

Concatenate Meaning

Concatenate in Java

In java, you can concatenate two different strings together using concat() function and also the same string to itself multiple times using + operator or respectively.

1. By + (string concatenation) operator

Java concatenation operator (+) is used to combine two string values to create one new string.

Example

JavaStringConcatenate.java

Output:

Concatenate Meaning

2. Java concat() method

The concat() method in Java appends the given second string to the end of the first string and stores it to a new string.

Syntax:

Parameters

  • str1 (required): This parameter represents the first string.
  • str2 (required): This parameter represents the second string that is concatenated to the end of the first string.

Example

Output:

Concatenate Meaning

Concatenation in PHP

1. '.' Operator

PHP string concatenation operator (.) adds two strings.

Example

Output:

Reema Panda

2. '.=' Operator

The PHP Concatenation assignment represented by '.=' operator is used to combine two strings.

Example

Output:

Hello World!

Concatenation in Python

1. Use the + (string concatenation) operator

The Python '+' (string concatenate) operator is used to combine two different strings together.

Example

Output:

Concatenate Meaning

2. Using join() method

The Python join() is a string method that concatenates the string with the elements of iterable.

Syntax

Parameters

strName: This represents the name of a string where the elements of iterable will be stored.

iterable: This parameter represents the objects that are capable of returning its elements one at a time. It could be List, Tuple, String, Set, etc.,

Return

The join() method in Python returns the concatenated string.

Example:

Output:

Concatenate Meaning

Concatenate in C#

1. Using '+' operator

In C# two strings can be concatenated using the '+' operator.

Example

Output:

Concatenate Meaning

2. Using the String.Concat Method

The String.Concat method in C# concatenates one or more instances of String and returns a concatenated string.

Syntax

Parameters

str1 (required): This parameter represents the first string to concatenate.

str2 (required): It represents the second string to concatenate.

Return

This method returns a concatenated string.

Example:

Output:

Concatenate Meaning
Next TopicEuphoria Meaning





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA