Trim

From SwiftAPI

Jump to: navigation, search

Remove whitespace from the beginning and end of a string.

Contents

C#

MSDN

System.String : string Trim();
string a = "    Hello World    ";
string b = a.Trim();
// b == "Hello World"

Java

docs.oracle.com

java.lang.String : public String trim()
String a = "    Hello World    ";
String b = a.trim();
// b == "Hello World"

JavaScript

var a = "    Hello World    ";
var b = a.replace(/^\s+|\s+$/g, "");
// b == "Hello World"

ECMAScript 5.1, MooTools

Firefox 3.5+, Internet Explorer 9+, Safari 5+, Chrome


Mozilla msdn mootools

var a = "    Hello World    ";
var b = a.trim();
// b == "Hello World"

jQuery

jQuery

var a = "    Hello World    ";
var b = $.trim(a);
// b == "Hello World"

Prototype

prototype

var a = "    Hello World    ";
var b = a.strip();
// b == "Hello World"


Objective-C

developer.apple.com

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set
NSString *a = "    Hello World    ";
NSString *b = [a stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// b == "Hello World"

Perl

Perl 5

$a = '    Hello World    ';
$a =~ s/^\s+|\s+$//g;
# $a == 'Hello World'

Perl 6

$a = '    Hello World    ';
$b = $a.trim;
# $b == 'Hello World'

PHP

php.net

string trim( string $str [, string $charlist ] )
$a = '    Hello World    ';
$b = trim($a);
// $b == 'Hello World'
$a = '----Hello World++++';
$b = trim($a, '-+');
// $b == 'Hello World'

Python

docs.python.org

a = '    Hello World    '
b = a.strip()
# b == 'Hello World'
a = '----Hello World++++'
b = a.strip("-+")
# b == 'Hello World'

Ruby

ruby-doc.org

a = '    Hello World    '
b = a.strip
# b == 'Hello World'
Personal tools