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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
87번째 줄: 87번째 줄:
<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
=CONCATENATE("ab", "cd", "ef") // returns "abcdef"
=CONCATENATE("ab", "cd", "ef") // returns "abcdef"
</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>
</syntaxhighlight>



2021년 4월 27일 (화) 00:00 판

  다른 뜻에 대해서는 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 D

"abc" ~ "def";      // returns "abcdef"

7 Excel

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

8 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
}

9 Java

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

10 JavaScript

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

11 Lua

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

12 Lisp

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

13 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

14 Pascal

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

15 Perl

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

16 PHP

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

17 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

18 Python

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

19 R

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

20 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

21 SQL

21.1 MySQL

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

21.2 Oracle

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

21.3 MS SQL

SELECT col1 + '' + col2 FROM mytable;

21.4 PostgreSQL

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

22 VB

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

23 같이 보기