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

147번째 줄: 147번째 줄:
$a = 'hello'
$a = 'hello'
$b = 'world'
$b = 'world'
$c = $a + $b
$a + $b
echo $c
echo $c
# helloworld
# helloworld
</source>
<source lang='PowerShell'>
$a = 1
$b = 2
$a + $b
# 3
</source>
<source lang='PowerShell'>
$a = 1
$b = 2
"" + $a + $b
# 12
</source>
</source>



2015년 3월 30일 (월) 18:14 판

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 PowerShell

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

16 Python

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

17 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

18 SQL

18.1 MySQL

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

18.2 Oracle

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

18.3 MS SQL

SELECT col1 + '' + col2 FROM mytable;

18.4 PostgreSQL

  • See Oracle

19 VB

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

20 같이 보기