"함수 trim()"의 두 판 사이의 차이

49번째 줄: 49번째 줄:
<source lang='javascript'>
<source lang='javascript'>
function trim(str) {
function trim(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  return .replace(/^\s*/, '').replace(/\s*$/, '');
}
}


56번째 줄: 56번째 줄:
</source>
</source>
<source lang='javascript'>
<source lang='javascript'>
String.prototype.trim=function(){return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');};
String.prototype.trim=function(){return this.replace(/^\s*/,'').replace(/\s*$/,'');};


var str = "  HELLO  ";
var str = "  HELLO  ";

2017년 8월 23일 (수) 20:49 판

trim
strip

1 Bash

STR="  Hello, World  "
echo "[$STR]"  # [  Hello, World  ]
TRIMMED=`echo $STR`
echo "[$TRIMMED]"  # [Hello, World]

2 Cmd

set str=  Hello  
echo [%str%]
for /f "tokens=* delims= " %a in ("%str%") do set str=%a
echo [%str%]
REM [  Hello  ]
REM [Hello   ]

3 C#

string str = "  HELLO  ";
string trimmed = str.Trim();

4 Excel

=TRIM("  HELLO  ")
// returns 'HELLO'

5 Java

String str = "  HELLO  ";
String trimmed = str.trim();

6 JavaScript

function trim(str) {
 return .replace(/^\s*/, '').replace(/\s*$/, '');
}

var str = "  HELLO  ";
var trimmed = trim(str);
String.prototype.trim=function(){return this.replace(/^\s*/,'').replace(/\s*$/,'');};

var str = "  HELLO  ";
var trimmed = str.trim();

7 jQuery

var trimmed = $.trim("  HELLO  ");

8 Objective-C

NSString *str = @"  HELLO  ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

9 PHP

$trimmed = trim("\t  hello  \n"); // returns "hello"

10 Python

trimmed = "\t  hello  \n".strip() # returns "hello"

11 Ruby

trimmed = "\t  hello  \n".strip # returns "hello"

12 SQL

12.1 MySQL

SELECT TRIM('  hello world  ');
SELECT TRIM("  hello world  ");
-- hello world

12.2 Oracle

LTRIM(RTRIM(" hello "));

13 같이 보기