"함수 concat()"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 6개는 보이지 않습니다)
11번째 줄: 11번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
str1="Hello,"
str1="Hello,"
str2=" World"
str2=" World"
17번째 줄: 17번째 줄:
echo $output
echo $output
# Hello, World
# Hello, World
</source>
</syntaxhighlight>


==C==
==C==
{{참고|C언어 concat()}}
{{참고|C언어 concat()}}
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> // malloc
#include <stdlib.h> // malloc
37번째 줄: 37번째 줄:
     printf(output); // Hello, World
     printf(output); // Hello, World
}
}
</source>
</syntaxhighlight>
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h> // malloc
#include <stdlib.h> // malloc
56번째 줄: 56번째 줄:
     printf(output); // Hello, World
     printf(output); // Hello, World
}
}
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
<syntaxhighlight lang="csharp">
"abc" + "def";      // returns "abcdef"
"abc" + "def";      // returns "abcdef"
</source>
</syntaxhighlight>


==Cmd==
==Cmd==
[[category: Cmd]]
[[category: Cmd]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
set "str1=Hello,"
set "str1=Hello,"
set "str2= World"
set "str2= World"
72번째 줄: 72번째 줄:
echo %output%
echo %output%
REM shows "Hello, World"
REM shows "Hello, World"
</source>
</syntaxhighlight>
 
==D==
[[category: D]]
<source lang="d">
"abc" ~ "def";      // returns "abcdef"
</source>


==Excel==
==Excel==
[[category: Excel]]
[[category: Excel]]
<source lang="php">
<syntaxhighlight lang="php">
="ab" & "cd" & "ef" // returns "abcdef"
="ab" & "cd" & "ef" // returns "abcdef"
</source>
</syntaxhighlight>
<source lang="php">
<syntaxhighlight lang="php">
=CONCATENATE("ab", "cd", "ef") // returns "abcdef"
=CONCATENATE("ab", "cd", "ef") // returns "abcdef"
</source>
</syntaxhighlight>
 
==Go==
[[분류: Go]]
{{참고|Go 문자열 합치기}}
<syntaxhighlight lang='go' run>
package main
import "fmt"
func main() {
s := "Hello"
s = s + "World"
fmt.Println(s) // HelloWorld
}
</syntaxhighlight>
<syntaxhighlight lang='go' run>
package main
import "fmt"
func main() {
a := "Hello"
b := "World"
c := a + b
fmt.Println(c) // HelloWorld
}
</syntaxhighlight>


==Java==
==Java==
[[category: Java]]
[[category: Java]]
<source lang="java">
<syntaxhighlight lang="java">
"abc" + "def"      // returns "abcdef"
"abc" + "def"      // returns "abcdef"
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang="javascript">
<syntaxhighlight lang="javascript" run>
"abc" + "def"     // returns "abcdef"
console.log( "abc" + "def" ); // abcdef
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang="javascript" run>
var str1 = "abc";
var str1 = "abc";
var str2 = "def";
var str2 = "def";
var output = str1.concat(str2); // abcdef
var output = str1.concat(str2);
</source>
console.log( output ); // abcdef
<source lang='javascript'>
</syntaxhighlight>
<syntaxhighlight lang="javascript" run>
var str1 = "abc";
var str1 = "abc";
var str2 = "def";
var str2 = "def";
var str3 = "ghi";
var str3 = "ghi";
var output = str1.concat(str2,str3); // abcdefghi
var output = str1.concat(str2,str3);
</source>
console.log( output ); // abcdefghi
</syntaxhighlight>


==Lua==
==Lua==
{{참고|Lua 문자열 합치기 ..}}
[[category: lua]]
[[category: lua]]
<source lang='lua'>
<syntaxhighlight lang='lua'>
print ( "abc" .. "def" )
print ( "abc" .. "def" )
-- abcdef
-- abcdef
print ( "abc" .. "def" .. "ghi" )
print ( "abc" .. "def" .. "ghi" )
-- abcdefghi
-- abcdefghi
</source>
</syntaxhighlight>
 


==Lisp==
==Lisp==
[[category: lisp]]
[[category: lisp]]
<source lang=lisp>
<syntaxhighlight lang=lisp>
(concatenate 'string "abc " "def " "ghi")  ; returns "abc def ghi"
(concatenate 'string "abc " "def " "ghi")  ; returns "abc def ghi"
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang="objc">
<syntaxhighlight lang="objc">
NSString* str = [@"abc" stringByAppendingString:@"def"];
NSString* str = [@"abc" stringByAppendingString:@"def"];
NSLog(@"%@", str); // abcdef
NSLog(@"%@", str); // abcdef
</source>
</syntaxhighlight>
<source lang='objc'>
<syntaxhighlight lang='objc'>
NSString* str = [NSString stringWithFormat:@"%@%@%@", @"ab", @"cd", @"ef"];
NSString* str = [NSString stringWithFormat:@"%@%@%@", @"ab", @"cd", @"ef"];
NSLog(@"%@", str); // abcdef
NSLog(@"%@", str); // abcdef
</source>
</syntaxhighlight>
<source lang='objc'>
<syntaxhighlight lang='objc'>
+(NSString*)concat:(NSString*) str, ... {
+(NSString*)concat:(NSString*) str, ... {
     NSString* arg;
     NSString* arg;
148번째 줄: 167번째 줄:
}
}
// NSString* str = [Util concat:@"ab", @"cd", @"ef", nil]; // abcdef
// NSString* str = [Util concat:@"ab", @"cd", @"ef", nil]; // abcdef
</source>
</syntaxhighlight>


==Pascal==
==Pascal==
[[category: Pascal]]
[[category: Pascal]]
<source lang="pascal">
<syntaxhighlight lang="pascal">
'abc' + 'def';      // returns "abcdef"
'abc' + 'def';      // returns "abcdef"
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang="perl">
<syntaxhighlight lang="perl">
'abc'.'def';      # returns "abcdef"
'abc'.'def';      # returns "abcdef"
</source>
</syntaxhighlight>
<source lang="perl">
<syntaxhighlight lang="perl">
$str1 = 'abc';
$str1 = 'abc';
$str2 = 'def';
$str2 = 'def';
167번째 줄: 186번째 줄:
print "$str1$str2"; # abcdef
print "$str1$str2"; # abcdef
print "hello $str1 $str2"; # hello abc def
print "hello $str1 $str2"; # hello abc def
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang="php">
<syntaxhighlight lang="php">
'abc'.'def';      // returns "abcdef"
'abc'.'def';      // returns "abcdef"
</source>
</syntaxhighlight>
<source lang="php">
<syntaxhighlight lang="php">
$str1 = 'abc';
$str1 = 'abc';
$str2 = 'def';
$str2 = 'def';
180번째 줄: 199번째 줄:
echo "$str1$str2"; // abcdef
echo "$str1$str2"; // abcdef
echo "hello $str1 $str2"; // hello abc def
echo "hello $str1 $str2"; // hello abc def
</source>
</syntaxhighlight>


==PowerShell==
==PowerShell==
[[category: PowerShell]]
[[category: PowerShell]]
<source lang='PowerShell'>
<syntaxhighlight lang='PowerShell'>
$a = 'hello'
$a = 'hello'
$b = 'world'
$b = 'world'
190번째 줄: 209번째 줄:
echo $c
echo $c
# helloworld
# helloworld
</source>
</syntaxhighlight>
<source lang='PowerShell'>
<syntaxhighlight lang='PowerShell'>
$a = 1
$a = 1
$b = 2
$b = 2
$a + $b
$a + $b
# 3
# 3
</source>
</syntaxhighlight>
<source lang='PowerShell'>
<syntaxhighlight lang='PowerShell'>
$a = 1
$a = 1
$b = 2
$b = 2
"" + $a + $b
"" + $a + $b
# 12
# 12
</source>
</syntaxhighlight>
<source lang='PowerShell'>
<syntaxhighlight lang='PowerShell'>
$a = '1'
$a = '1'
$b = '2'
$b = '2'
$a + $b
$a + $b
# 12
# 12
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang="python">
<syntaxhighlight lang="python">
'abc' + 'def'      # returns "abcdef"
'abc' + 'def'      # returns "abcdef"
</source>
</syntaxhighlight>


==R==
==R==
[[분류: R]]
[[분류: R]]
{{참고|R paste()}}
{{참고|R paste()}}
<source lang='r'>
<syntaxhighlight lang='r'>
paste("abc","def",sep="")
paste("abc","def",sep="")
## [1] "abcdef"
## [1] "abcdef"
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby">
puts 'abc' + 'def'
puts 'abc' + 'def'
# abcdef
# abcdef
237번째 줄: 256번째 줄:
puts "My name is #{name}"
puts "My name is #{name}"
# My name is Snoopy
# My name is Snoopy
</source>
</syntaxhighlight>


==SQL==
==SQL==
243번째 줄: 262번째 줄:
===MySQL===
===MySQL===
[[category: MySQL]]
[[category: MySQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT CONCAT('Hello','World');
SELECT CONCAT('Hello','World');
-- HelloWorld
-- HelloWorld
</source>
</syntaxhighlight>
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT CONCAT(col1, col2) FROM mytable;
SELECT CONCAT(col1, col2) FROM mytable;
UPDATE mytable SET col1 = CONCAT('Hello', col1) WHERE 1;
UPDATE mytable SET col1 = CONCAT('Hello', col1) WHERE 1;
</source>
</syntaxhighlight>


===Oracle===
===Oracle===
[[category: Oracle]]
[[category: Oracle]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT col1 || '' || col2 FROM mytable;
SELECT col1 || '' || col2 FROM mytable;
</source>
</syntaxhighlight>


===MS SQL===
===MS SQL===
[[category: MS SQL]]
[[category: MS SQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT col1 + '' + col2 FROM mytable;
SELECT col1 + '' + col2 FROM mytable;
</source>
</syntaxhighlight>


===PostgreSQL===
===PostgreSQL===
[[category: PostgreSQL]]
[[category: PostgreSQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT col1 || '' || col2 FROM mytable;
SELECT col1 || '' || col2 FROM mytable;
</source>
</syntaxhighlight>


==VB==
==VB==
[[category: VB]]
[[category: VB]]
<source lang="vb">
<syntaxhighlight lang="vb">
"abc" & "def"      '  returns "abcdef"
"abc" & "def"      '  returns "abcdef"
"abc" + "def"      '  returns "abcdef"
"abc" + "def"      '  returns "abcdef"
"abc" & Null        '  returns "abc"
"abc" & Null        '  returns "abc"
"abc" + Null        '  returns Null
"abc" + Null        '  returns Null
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 4월 27일 (화) 00:01 기준 최신판

  다른 뜻에 대해서는 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 같이 보기[ | ]