함수 concat()

Jmnote (토론 | 기여)님의 2015년 1월 20일 (화) 22:15 판 (→‎See also)
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#

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

4 Cmd

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

5 D

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

6 Excel

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

7 Java

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

8 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

9 Lua

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


10 Lisp

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

11 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

12 Pascal

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

13 Perl

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

14 PHP

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

15 Python

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

16 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

17 SQL

17.1 MySQL

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

17.2 Oracle

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

17.3 MS SQL

SELECT col1 + '' + col2 FROM mytable;

17.4 PostgreSQL

  • See Oracle

18 VB

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

19 같이 보기