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++ substr() 문서를 참고하십시오.
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[ | ]
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 substring() 문서를 참고하십시오.
R substr() 문서를 참고하십시오.
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 같이 보기[ | ]
- slice
- charAt
- utf8_substr
- Substring_index
- string_length
- 함수 substr_count()
- 함수 substr_after()
- 함수 substr_before()
- 함수 substr_from()
- 함수 substr_until()
- 함수 rotateString()
- 리눅스 cut
- SQL 함수
- 미디어위키 substring
18 References[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.
리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― …리눅스 Python 2.7 컴파일 설치 ― Jmnote리눅스 Python 2.7 컴파일 설치 ― ㅇㅇㅇ미운코딩새끼 ― 승호 도령미운코딩새끼 ― 불탄고등어미운코딩새끼 ― 김레이미운코딩새끼 ― 호박이미운코딩새끼 ― Junhg0211미운코딩새끼 ― 김왼손미운코딩새끼 ― 용딘이미운코딩새끼 ― Pinkcrimson유기농냠냠파이썬 ― 호박유기농냠냠파이썬 ― 이에스유기농냠냠파이썬 ― 이승현파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― John Jeong파이썬 global ― Jmnote파이썬 global ― John Jeong