함수 substring()

(Substr에서 넘어옴)

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
#include <iostream>
using namespace std;
int main() {
	string str = "abcdefghijk";
	string sub1 = str.substr(4,2);
	cout << sub1 << endl; // ef
	string sub2 = str.substr(6); 
	cout << sub2 << endl; // ghijk
}

4 C#[ | ]

C#
Copy
string str = "abcdefghijk";

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

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

5 Erlang[ | ]

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

6 Excel[ | ]

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

7 Go[ | ]

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

9 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

10 Lua[ | ]

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

11 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; // ★あ中

12 Perl[ | ]

Perl
Copy
print substr 'hello world', 1 . "\n";     # ello world
print substr 'hello world', 3 . "\n";     # lo world

print substr 'hello world', 7, 2 . "\n";  # or
print substr 'hello world', 7, 8 . "\n";  # orld

print substr 'hello world', -1 . "\n";    # d
print substr 'hello world', -2 . "\n";    # ld
print substr 'hello world', -2, 1 . "\n"; # l
print substr 'hello world', -3, 2 . "\n"; # rl

print substr 'hello world', 0, -1 . "\n";  # hello worl
print substr 'hello world', 2, -1 . "\n";  # llo worl
print substr 'hello world', 4, -4 . "\n";  # false
print substr 'hello world', -3, -1 . "\n"; # rl

13 Python[ | ]

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

14 R[ | ]

R
Copy
substring("abcdefghijk",5,6)
## [1] "ef"
substr("abcdefghijk",5,6)
## [1] "ef"

substring("abcdefghijk",7)
## [1] "ghijk"
#substr("abcdefghijk",7)

15 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****" */

16 SQL[ | ]

16.1 MySQL/MariaDB[ | ]

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

17 같이 보기[ | ]

18 References[ | ]