Can I concatenate more than two strings?

Yes, you can concatenate more than two strings in various programming languages. Here are examples in a few languages:

Python

You can use the + operator or the join() method:

str1 = "Hello"
str2 = " "
str3 = "World"
str4 = "!"

# Using + operator
result = str1 + str2 + str3 + str4
print(result)  # Output: Hello World!

# Using join() method
result = ''.join([str1, str2, str3, str4])
print(result)  # Output: Hello World!

Java

In Java, you can use the + operator or StringBuilder:

public class Main {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = " ";
        String str3 = "World";
        String str4 = "!";

        // Using + operator
        String result = str1 + str2 + str3 + str4;
        System.out.println(result);  // Output: Hello World!

        // Using StringBuilder
        StringBuilder sb = new StringBuilder();
        sb.append(str1).append(str2).append(str3).append(str4);
        System.out.println(sb.toString());  // Output: Hello World!
    }
}

C

In C, you can use strcat from the string.h library:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[50] = "Hello";
    char str2[] = " ";
    char str3[] = "World";
    char str4[] = "!";

    strcat(str1, str2);
    strcat(str1, str3);
    strcat(str1, str4);

    printf("%s\n", str1);  // Output: Hello World!

    return 0;
}

You can concatenate as many strings as you need using these methods.

0 Comments

no data
Be the first to share your comment!