함수 trim()

trim
strip

1 Bash[ | ]

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

2 C++[ | ]

#include <iostream>
#include <vector>
#include <algorithm> 
using namespace std;

int main() {
    string s = "  Hello, World  ";
    s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char ch) { return !isspace(ch); }));
    s.erase(find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !isspace(ch); }).base(), s.end());
    cout << '[' << s << ']'; // [Hello, World]
}

3 C#[ | ]

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

4 Cmd[ | ]

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

5 Excel[ | ]

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

6 Go[ | ]

package main

import (
    "fmt"
    "strings"
)

func main() {
    str := "  HELLO  ";
    trimmed := strings.Trim(str," ");
    fmt.Println("["+str+"]");
    fmt.Println("["+trimmed+"]");
}

7 Java[ | ]

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

8 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();

9 jQuery[ | ]

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

10 Objective-C[ | ]

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

11 PHP[ | ]

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

12 Python[ | ]

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

13 Perl[ | ]

my $trimmed="   hello   ";
$trimmed =~ s/^\s+|\s+$//g;
use String::Util qw(trim);
$trimmed = trim("   hello   ");

14 R[ | ]

s = "  Hello, World  "
print(s)
## [1] "  Hello, World  "
trimmed = gsub("^\\s+|\\s+$", "", s)
print(trimmed)
## [1] "Hello, World"

15 Ruby[ | ]

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

16 SQL[ | ]

16.1 MySQL[ | ]

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

16.2 Oracle[ | ]

LTRIM(RTRIM(" hello "));

17 같이 보기[ | ]