함수 left()

1 Overview[ | ]

left
  • Definition: left(string,n) returns string
  • Description: Returns the left n part of a string. If n is greater than the length of the string then most implementations return the whole string (exceptions exist - see code examples).

2 Bash[ | ]

input="Hello world"
output=${input:0:4}
echo $output
# Hell

3 POSIX Shell[ | ]

input="Hello world"
output=`echo $input | cut -c 1-4`
echo $output
# Hell

4 EXCEL[ | ]

=LEFT("abcde", 3) 
// abc

=LEFT("abcde", 8) 
// abcde

=LEFT("안녕하세요", 2)
// 안녕

=LEFT("안녕하세요")
// 안

5 Java[ | ]

String str = "Hello";
String a = StringUtils.left(str, 2);

6 JavaScript[ | ]

str = "Hello";
console.log( str.substr( 0, 2 ) ); // He
console.log( str.substring( 0, 2 ) ); // He
String.prototype.left = function(n) {
	return this.substring(0, n);
}
document.write("hello".left(2)); // he

7 PHP[ | ]

substr("abcde", 0, 3)         // returns "abc"
substr("abcde", 0, 8)         // returns "abcde"

8 Perl[ | ]

print substr "abcde", 0, 3 . "\n"; # returns "abc"
print substr "abcde", 0, 8 . "\n"; # returns "abcde"

9 Rexx[ | ]

left("abcde", 3)         /* returns "abc"      */
left("abcde", 8)         /* returns "abcde   " */
left("abcde", 8, "*")    /* returns "abcde***" */

10 Scheme[ | ]

(use-modules (srfi srfi-13))
(string-take "abcde", 3) ;  returns "abc" 
(string-take "abcde", 8) ;  error

11 Windows Batch[ | ]

set input=Hello World
set output=%input:~0,4%
echo %output%
REM Hell

12 SQL[ | ]

12.1 MySQL[ | ]

SELECT LEFT("hello world", 3);
-- hel

12.2 Oracle[ | ]

  • Oracle 8i, Oracle 9i, Oracle 10g, Oracle 11g
rpad('hello', 7); 
--returns 'hello  '
 
rpad('hello', 4); 
--returns 'hell' 

rpad('hello', 7, 'x'); 
--returns 'helloxx' 

rpad('hello', 4, 'x'); 
--returns 'hell'
select rpad('hello', 10, 'abc') from dual; 
--returns 'helloabcab'

13 VB[ | ]

Left("sandroguidi", 3)   
'  returns "san" 

Left("sandroguidi", 100) 
'  returns "sandroguidi"

14 같이 보기[ | ]

15 References[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}