함수 substring()

Jmnote (토론 | 기여)님의 2017년 6월 22일 (목) 20:44 판


1 Overview

substr
Substring
Definition
substring(string, startpos, endpos) returns string
substr(string, startpos, numChars) returns string
Description
Returns a substring of string between starting at startpos and endpos, or starting at startpos of length numChars. The resulting string is truncated if there are fewer than numChars characters beyond the starting point. endpos represents the index after the last character in the substring.

2 Bash

STR=abcdefg

SUB=${STR:3:2}
echo $SUB
# de

echo ${STR:3}
# defg
STR=abcdefg

echo $STR | cut -c 4-5
# de

echo $STR | cut -c 4-
# defg

3 C#

string str = "abcdefghijk";

string sub = str.Substring(4, 2);
// ef

sub = str.Substring(6);
// ghijk

4 Erlang

string:substr("abc", 2, 1). %  returns "b"
string:substr("abc", 2).    %  returns "bc"

5 Excel

=MID("hello",1,2)
// he
=MID("hello",1,4)
// hell
=MID("hello",3,2)
// ll

6 Java

System.out.println( "abcdefghijk".substring(2) ); // "cdefghijk"
System.out.println( "abcdefghijk".substring(3) ); // "defghijk"
System.out.println( "abcdefghijk".substring(11) ); // ""

System.out.println( "abcdefghijk".substring(4, 8) ); // "efgh"
System.out.println( "abcdefghijk".substring(1, 5) ); // "bcde"

7 JavaScript

console.log( "Hello world!".substr(2,3) ); // llo
var str = "A가★あ中!";
var output = str.substr(2,3);
document.write(output); // ★あ中
console.log( "Hello world!".substring(2,3) ); // l

8 Lua

print( string.sub("hello, world", 2, 4) )
-- ell
print( string.sub("hello, world", 4) )
-- lo, world
print( string.sub("hello, world", -4) )
-- orld
print( string.sub("hello, world", -4, -3) )
-- or

9 PHP

echo substr('hello world', 1);     // ello world
echo substr('hello world', 3);     // lo world

echo substr('hello world', 7, 2);  // or
echo substr('hello world', 7, 8);  // orld

echo substr('hello world', -1);    // d
echo substr('hello world', -2);    // ld
echo substr('hello world', -2, 1); // l
echo substr('hello world', -3, 2); // rl

echo substr('hello world', 0, -1);  // hello worl
echo substr('hello world', 2, -1);  // llo worl
echo substr('hello world', 4, -4);  // false
echo substr('hello world', -3, -1); // rl
$str = "A가★あ中!@";
$output = mb_substr($str,2,3,'UTF-8');
echo $output; // ★あ中
function utf8_substr($str, $start, $len=-1) {
  if($len==-1)$len = mb_strlen($str, 'UTF-8')-$start;
  return mb_substr($str, $start, $len, 'UTF-8');
}
$str = "A가★あ中!@";
$output = utf8_substr($str,2,3);
echo $output; // ★あ中

10 Python

"abc"[1:2]                 #  returns "b"
"abc"[1:3]                 #  returns "bc"

11 REXX

substr("abc", 2, 1)         /* returns "b"      */
substr("abc", 2)            /* returns "bc"     */
substr("abc", 2, 6)         /* returns "bc    " */
substr("abc", 2, 6, "*")    /* returns "bc****" */

12 SQL

12.1 MySQL

SELECT MID( "abcdefghijk", 5, 2 );
SELECT SUBSTR( "abcdefghijk", 5, 2 );
SELECT SUBSTRING( "abcdefghijk", 5, 2 );
# ef
SELECT MID( "abcdefghijk", 7);
SELECT SUBSTR( "abcdefghijk", 7);
SELECT SUBSTRING( "abcdefghijk", 7);
# ghijk
SELECT MID('감사합니다', 1,2);
SELECT SUBSTR('감사합니다', 1,2);
SELECT SUBSTRING('감사합니다', 1,2);
# 감사

13 같이 보기

14 References