함수 concat()

(함수 concat에서 넘어옴)
  다른 뜻에 대해서는 array_merge() 문서를 참조하십시오.
concat
Concatenate
operator + & . ~ ||

1 Overview[ | ]

  • Definition: concatenate(string1,string2) returns string.
  • Description: Concatenates (joins) two strings to each other, returning the combined string. Note that some languages like C have mutable strings, so really the second string is being appended to the first string and the mutated string is returned.

2 Bash[ | ]

str1="Hello,"
str2=" World"
output=$str1$str2
echo $output
# Hello, World

3 C[ | ]

#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // strlen
char* concat(const char *s1, const char *s2) {
    char *res = malloc(strlen(s1) + strlen(s2) + 1);
    strcpy(res, s1);
    strcat(res, s2);
    return res;
}
int main() {
    char* str1 = "Hello,";
    char* str2 = " World";
    char* output = concat(str1, str2);
    printf(output); // Hello, World
}
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h> // strlen
char* concat(const char *s1, const char *s2) {
    size_t len1 = strlen(s1);
    size_t len2 = strlen(s2);
    char *res = malloc(len1 + len2 + 1);
    memcpy(res, s1, len1);
    memcpy(res + len1, s2, len2 + 1);
    return res;
}
int main() {
    char* str1 = "Hello,";
    char* str2 = " World";
    char* output = concat(str1, str2);
    printf(output); // Hello, World
}

4 C#[ | ]

"abc" + "def";      // returns "abcdef"

5 Cmd[ | ]

set "str1=Hello,"
set "str2= World"
set "output=%str1%%str2%"
echo %output%
REM shows "Hello, World"

6 Excel[ | ]

="ab" & "cd" & "ef" // returns "abcdef"
=CONCATENATE("ab", "cd", "ef") // returns "abcdef"

7 Go[ | ]

package main
import "fmt"
func main() {
	s := "Hello"
	s = s + "World"
	fmt.Println(s) // HelloWorld
}
package main
import "fmt"
func main() {
	a := "Hello"
	b := "World"
	c := a + b
	fmt.Println(c) // HelloWorld
}

8 Java[ | ]

"abc" + "def"      // returns "abcdef"

9 JavaScript[ | ]

console.log( "abc" + "def" ); // abcdef
var str1 = "abc";
var str2 = "def";
var output = str1.concat(str2);
console.log( output ); // abcdef
var str1 = "abc";
var str2 = "def";
var str3 = "ghi";
var output = str1.concat(str2,str3);
console.log( output ); // abcdefghi

10 Lua[ | ]

print ( "abc" .. "def" )
-- abcdef
print ( "abc" .. "def" .. "ghi" )
-- abcdefghi

11 Lisp[ | ]

(concatenate 'string "abc " "def " "ghi")  ; returns "abc def ghi"

12 Objective-C[ | ]

NSString* str = [@"abc" stringByAppendingString:@"def"];
NSLog(@"%@", str); // abcdef
NSString* str = [NSString stringWithFormat:@"%@%@%@", @"ab", @"cd", @"ef"];
NSLog(@"%@", str); // abcdef
+(NSString*)concat:(NSString*) str, ... {
    NSString* arg;
    va_list args;
    va_start(args, str);
    while((arg = va_arg(args, NSString*))) str = [str stringByAppendingString:arg];
    va_end(args);
    return str;
}
// NSString* str = [Util concat:@"ab", @"cd", @"ef", nil]; // abcdef

13 Pascal[ | ]

'abc' + 'def';      // returns "abcdef"

14 Perl[ | ]

'abc'.'def';      # returns "abcdef"
$str1 = 'abc';
$str2 = 'def';
print $str1.$str2; # abcdef
print "$str1$str2"; # abcdef
print "hello $str1 $str2"; # hello abc def

15 PHP[ | ]

'abc'.'def';      // returns "abcdef"
$str1 = 'abc';
$str2 = 'def';
echo $str1.$str2; // abcdef
echo "$str1$str2"; // abcdef
echo "hello $str1 $str2"; // hello abc def

16 PowerShell[ | ]

$a = 'hello'
$b = 'world'
$a + $b
echo $c
# helloworld
$a = 1
$b = 2
$a + $b
# 3
$a = 1
$b = 2
"" + $a + $b
# 12
$a = '1'
$b = '2'
$a + $b
# 12

17 Python[ | ]

'abc' + 'def'      # returns "abcdef"

18 R[ | ]

paste("abc","def",sep="")
## [1] "abcdef"

19 Ruby[ | ]

puts 'abc' + 'def'
# abcdef

name = "Snoopy"
puts "My name is " + name
# My name is Snoopy
puts "My name is " << name
# My name is Snoopy
puts "My name is #{name}"
# My name is Snoopy

20 SQL[ | ]

20.1 MySQL[ | ]

SELECT CONCAT('Hello','World');
-- HelloWorld
SELECT CONCAT(col1, col2) FROM mytable;
UPDATE mytable SET col1 = CONCAT('Hello', col1) WHERE 1;

20.2 Oracle[ | ]

SELECT col1 || '' || col2 FROM mytable;

20.3 MS SQL[ | ]

SELECT col1 + '' + col2 FROM mytable;

20.4 PostgreSQL[ | ]

SELECT col1 || '' || col2 FROM mytable;

21 VB[ | ]

"abc" & "def"       '  returns "abcdef"
"abc" + "def"       '  returns "abcdef"
"abc" & Null        '  returns "abc"
"abc" + Null        '  returns Null

22 같이 보기[ | ]