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

 
(사용자 3명의 중간 판 4개는 보이지 않습니다)
6번째 줄: 6번째 줄:
==Bash==
==Bash==
[[category:Bash]]
[[category:Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
STR="  Hello, World  "
STR="  Hello, World  "
echo "[$STR]"  # [  Hello, World  ]
echo "[$STR]"  # [  Hello, World  ]
TRIMMED=`echo $STR`
TRIMMED=`echo $STR`
echo "[$TRIMMED]"  # [Hello, World]
echo "[$TRIMMED]"  # [Hello, World]
</source>
</syntaxhighlight>
 
==C++==
[[분류:C++]]
{{참고|C++ trim() 구현}}
<syntaxhighlight lang='cpp' run>
#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]
}
</syntaxhighlight>
 
==C#==
[[category:Csharp]]
<syntaxhighlight lang='csharp'>
string str = "  HELLO  ";
string trimmed = str.Trim();
</syntaxhighlight>


==Cmd==
==Cmd==
[[category:Cmd]]
[[category:Cmd]]
<source lang='dos'>
<syntaxhighlight lang='dos'>
set str=  Hello   
set str=  Hello   
echo [%str%]
echo [%str%]
22번째 줄: 46번째 줄:
REM [  Hello  ]
REM [  Hello  ]
REM [Hello  ]
REM [Hello  ]
</source>
</syntaxhighlight>
 
==C#==
[[category:Csharp]]
<source lang='csharp'>
string str = "  HELLO  ";
string trimmed = str.Trim();
</source>


==Excel==
==Excel==
[[category:Excel]]
[[category:Excel]]
<source lang='php'>
<syntaxhighlight lang='php'>
=TRIM("  HELLO  ")
=TRIM("  HELLO  ")
// returns 'HELLO'
// returns 'HELLO'
</source>
</syntaxhighlight>
 
==Go==
[[분류: Go]]
{{참고|Go Trim()}}
<syntaxhighlight lang='go'>
package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    str := "  HELLO  ";
    trimmed := strings.Trim(str," ");
    fmt.Println("["+str+"]");
    fmt.Println("["+trimmed+"]");
}
</syntaxhighlight>


==Java==
==Java==
[[category:Java]]
[[category:Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
String str = "  HELLO  ";
String str = "  HELLO  ";
String trimmed = str.trim();
String trimmed = str.trim();
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category:JavaScript]]
[[category:JavaScript]]
{{참고|JavaScript trim()}}
{{참고|JavaScript trim()}}
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
function trim(str) {
function trim(str) {
  return .replace(/^\s*/, '').replace(/\s*$/, '');
  return .replace(/^\s*/, '').replace(/\s*$/, '');
55번째 줄: 91번째 줄:
var str = "  HELLO  ";
var str = "  HELLO  ";
var trimmed = trim(str);
var trimmed = trim(str);
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
String.prototype.trim=function(){return this.replace(/^\s*/,'').replace(/\s*$/,'');};
String.prototype.trim=function(){return this.replace(/^\s*/,'').replace(/\s*$/,'');};


var str = "  HELLO  ";
var str = "  HELLO  ";
var trimmed = str.trim();
var trimmed = str.trim();
</source>
</syntaxhighlight>


==jQuery==
==jQuery==
[[category: jQuery]]
[[category: jQuery]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var trimmed = $.trim("  HELLO  ");
var trimmed = $.trim("  HELLO  ");
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category:Objective-C]]
[[category:Objective-C]]
<source lang='objc'>
<syntaxhighlight lang='objc'>
NSString *str = @"  HELLO  ";
NSString *str = @"  HELLO  ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category:PHP]]
[[category:PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$trimmed = trim("\t  hello  \n"); // returns "hello"
$trimmed = trim("\t  hello  \n"); // returns "hello"
</source>
</syntaxhighlight>


==Python==
==Python==
[[category:Python]]
[[category:Python]]
<source lang='python'>
<syntaxhighlight lang='python'>
trimmed = "\t  hello  \n".strip() # returns "hello"
trimmed = "\t  hello  \n".strip() # returns "hello"
</source>
</syntaxhighlight>
 
==Perl==
[[category: Perl]]
<syntaxhighlight lang='Perl'>
my $trimmed="  hello  ";
$trimmed =~ s/^\s+|\s+$//g;
</syntaxhighlight>
<syntaxhighlight lang='Perl'>
use String::Util qw(trim);
$trimmed = trim("  hello  ");
</syntaxhighlight>
 
==R==
[[category:R]]
{{참고|R trim()}}
<syntaxhighlight lang='r'>
s = "  Hello, World  "
print(s)
## [1] "  Hello, World  "
trimmed = gsub("^\\s+|\\s+$", "", s)
print(trimmed)
## [1] "Hello, World"
</syntaxhighlight>


==Ruby==
==Ruby==
[[category:Ruby]]
[[category:Ruby]]
<source lang='ruby'>
<syntaxhighlight lang='ruby'>
trimmed = "\t  hello  \n".strip # returns "hello"
trimmed = "\t  hello  \n".strip # returns "hello"
</source>
</syntaxhighlight>


==SQL==
==SQL==
98번째 줄: 157번째 줄:
===MySQL===
===MySQL===
[[category:MySQL]]
[[category:MySQL]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
SELECT TRIM('  hello world  ');
SELECT TRIM('  hello world  ');
SELECT TRIM("  hello world  ");
SELECT TRIM("  hello world  ");
-- hello world
-- hello world
</source>
</syntaxhighlight>


===Oracle===
===Oracle===
[[category:Oracle]]
[[category:Oracle]]
<source lang='sql'>
<syntaxhighlight lang='sql'>
LTRIM(RTRIM(" hello "));  
LTRIM(RTRIM(" hello "));  
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2023년 9월 24일 (일) 15:18 기준 최신판

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 같이 보기[ | ]