함수 substring()

Jmnote (토론 | 기여)님의 2016년 4월 22일 (금) 18:08 판 (→‎같이 보기)


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

Bash
Copy
STR=abcdefg

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

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

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

echo $STR | cut -c 4-
# defg

3 C#

C#
Copy
string str = "abcdefghijk";

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

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

4 Erlang

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

5 Excel

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

6 Java

Java
Copy
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

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

8 PHP

PHP
Copy
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
PHP
Copy
$str = "A가★あ中!@";
$output = mb_substr($str,2,3,'UTF-8');
echo $output; // ★あ中
PHP
Copy
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; // ★あ中

9 Python

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

10 REXX

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

11 SQL

11.1 MySQL

sql
Copy
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

12 같이 보기

13 References