Strip whitespace from a string/Top and tail: Difference between revisions
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(66 intermediate revisions by 40 users not shown) | |||
Line 11: | Line 11: | ||
<br> |
<br> |
||
For the purposes of this task whitespace includes non printable characters such as the space character, the tab character, and other such characters that have no corresponding graphical representation. |
For the purposes of this task whitespace includes non printable characters such as the space character, the tab character, and other such characters that have no corresponding graphical representation. |
||
{{Template:Strings}} |
|||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
<syntaxhighlight lang="11l">V s = " \t \r \n String with spaces \t \r \n " |
|||
print(s.ltrim((‘ ’, "\t", "\r", "\n")).len) |
|||
print(s.rtrim((‘ ’, "\t", "\r", "\n")).len) |
|||
print(s.trim((‘ ’, "\t", "\r", "\n")).len)</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
29 |
|||
25 |
|||
18 |
|||
</pre> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">DEFINE SPACE="32" |
|||
DEFINE TAB="127" |
|||
DEFINE NEWLINE="155" |
|||
BYTE FUNC IsWhitespace(CHAR c) |
|||
IF c=SPACE OR c=TAB OR c=NEWLINE THEN |
|||
RETURN (1) |
|||
FI |
|||
RETURN (0) |
|||
PROC Strip(CHAR ARRAY src,dst BYTE head,tail) |
|||
BYTE i,first,last |
|||
first=1 |
|||
last=src(0) |
|||
IF head THEN |
|||
WHILE first<=last |
|||
DO |
|||
IF IsWhitespace(src(first))=0 THEN |
|||
EXIT |
|||
FI |
|||
first==+1 |
|||
OD |
|||
FI |
|||
IF tail THEN |
|||
WHILE last>=first |
|||
DO |
|||
IF IsWhitespace(src(last))=0 THEN |
|||
EXIT |
|||
FI |
|||
last==-1 |
|||
OD |
|||
FI |
|||
IF first>last THEN |
|||
dst(0)=0 |
|||
ELSE |
|||
SCopyS(dst,src,first,last) |
|||
FI |
|||
RETURN |
|||
PROC Main() |
|||
CHAR ARRAY src=" Action! ",dst(20) |
|||
src(2)=NEWLINE src(13)=TAB |
|||
PrintF("Original string:%E""%S""%E%E",src) |
|||
Strip(src,dst,1,0) |
|||
PrintF("Trim leading whitespaces:%E""%S""%E%E",dst) |
|||
Strip(src,dst,0,1) |
|||
PrintF("Trim trailing whitespaces:%E""%S""%E%E",dst) |
|||
Strip(src,dst,1,1) |
|||
PrintF("Trim leading and trailing whitespaces:""%S""%E%E",dst) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strip_whitespace_from_a_string_top_and_tail.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Original string: |
|||
" |
|||
Action! " |
|||
Trim leading whitespaces: |
|||
"Action! " |
|||
Trim trailing whitespaces: |
|||
" |
|||
Action!" |
|||
Trim leading and trailing whitespaces: |
|||
"Action!" |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Strings; use Ada.Strings; |
with Ada.Strings; use Ada.Strings; |
||
with Ada.Strings.Fixed; use Ada.Strings.Fixed; |
with Ada.Strings.Fixed; use Ada.Strings.Fixed; |
||
Line 24: | Line 115: | ||
Put_Line ("'" & Trim (str, Right) & "'"); |
Put_Line ("'" & Trim (str, Right) & "'"); |
||
Put_Line ("'" & Trim (str, Both) & "'"); |
Put_Line ("'" & Trim (str, Both) & "'"); |
||
end StripDemo;</ |
end StripDemo;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 31: | Line 122: | ||
'Jabberwocky' |
'Jabberwocky' |
||
</pre> |
</pre> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
=== version 1 === |
|||
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}} |
||
< |
<syntaxhighlight lang="algol68"># returns "text" with leading non-printing characters removed # |
||
PROC trim leading whitespace = ( STRING text )STRING: |
PROC trim leading whitespace = ( STRING text )STRING: |
||
BEGIN |
BEGIN |
||
Line 86: | Line 179: | ||
print( ( "trim trailing: """ + trim trailing whitespace( test ) + """", newline ) ); |
print( ( "trim trailing: """ + trim trailing whitespace( test ) + """", newline ) ); |
||
print( ( "trim both: """ + trim whitespace ( test ) + """", newline ) ) |
print( ( "trim both: """ + trim whitespace ( test ) + """", newline ) ) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 94: | Line 187: | ||
</pre> |
</pre> |
||
=== |
=== version 2 === |
||
{{works with|ALGOL 68G|2.8.3}} |
{{works with|ALGOL 68G|2.8.3}} |
||
A single procedure that trims leading space, trailing space, or both. |
A single procedure that trims leading space, trailing space, or both. |
||
< |
<syntaxhighlight lang="algol68"># |
||
string_trim |
string_trim |
||
Trim leading and trailing whitespace from string. |
Trim leading and trailing whitespace from string. |
||
Line 127: | Line 220: | ||
print(("string_trim(' foobar '): expected 'foobar'; actual: " |
print(("string_trim(' foobar '): expected 'foobar'; actual: " |
||
+ string_trim(" foobar "), newline)) FI |
+ string_trim(" foobar "), newline)) FI |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Amazing Hopper}}== |
|||
<p>Amazing Hopper! Flavour "Jambo".</p> |
|||
<syntaxhighlight lang="Amazing Hopper"> |
|||
#include <jambo.h> |
|||
Main |
|||
Set stack 25 |
|||
c = "\t\t\n \n Message to triming\t\n \t" |
|||
Printnl ("Original Message: [", c,"]") |
|||
Set '"\n\n\UL\ENFNatural syntax:\n\n"', then bold off, and underline off |
|||
Set '"Right trim: [", c', Do right trim; then set '"]"' |
|||
Set '"\nLeft trim: [", c', Do left trim; Set '"]"' |
|||
Set '"\nAll trim: [", c', Do trim, now set '"]"' and print with newline |
|||
Underline( Bold( "\n\nFormal syntax:\n\n" )) |
|||
Printnl ( "Right trim: [", Rtrim(c),\ |
|||
"]\nLeft trim: [", Ltrim(c),\ |
|||
"]\nAll trim: [",Trim(c),"]\n" ) |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Original Message: [ |
|||
Message to triming |
|||
] |
|||
Natural syntax: |
|||
Right trim: [ |
|||
Message to triming] |
|||
Left trim: [Message to triming |
|||
] |
|||
All trim: [Message to triming] |
|||
Formal syntax: |
|||
Right trim: [ |
|||
Message to triming] |
|||
Left trim: [Message to triming |
|||
] |
|||
All trim: [Message to triming] |
|||
</pre> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
< |
<syntaxhighlight lang="applescript">use framework "Foundation" -- "OS X" Yosemite onwards, for NSRegularExpression |
||
-- STRIP WHITESPACE ---------------------------------------------------------- |
-- STRIP WHITESPACE ---------------------------------------------------------- |
||
Line 248: | Line 396: | ||
|error|:(missing value))'s firstMatchInString:oString options:0 ¬ |
|error|:(missing value))'s firstMatchInString:oString options:0 ¬ |
||
range:{location:0, |length|:oString's |length|()}) is not missing value |
range:{location:0, |length|:oString's |length|()}) is not missing value |
||
end regexTest</ |
end regexTest</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="applescript">{"-->Much Ado About Nothing |
||
<--", "--> |
<--", "--> |
||
Much Ado About Nothing<--", "-->Much Ado About Nothing<--"}</ |
Much Ado About Nothing<--", "-->Much Ado About Nothing<--"}</syntaxhighlight> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">str: " Hello World " |
|||
print [pad "strip all:" 15 ">" strip str "<"] |
|||
print [pad "strip leading:" 15 ">" strip.start str "<"] |
|||
print [pad "strip trailing:" 15 ">" strip.end str "<"]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> strip all: > Hello World < |
|||
strip leading: > Hello World < |
|||
strip trailing: > Hello World <</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
AutoHotkey_L and v2 contain a [http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/commands/Trim.htm Trim function] |
AutoHotkey_L and v2 contain a [http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/commands/Trim.htm Trim function] |
||
< |
<syntaxhighlight lang="autohotkey">string := " abc " |
||
MsgBox % clipboard := "<" LTrim(string) ">`n<" RTrim(string) ">`n<" . Trim(string) ">"</ |
MsgBox % clipboard := "<" LTrim(string) ">`n<" RTrim(string) ">`n<" . Trim(string) ">"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre><abc > |
<pre><abc > |
||
Line 268: | Line 430: | ||
*[:graph:] is a POSIX character class for characters that are both printable and visible. The following strips the entire non-[:graph:] character set. A simplified version for the more common task of removing only spaces and tabs follows it. |
*[:graph:] is a POSIX character class for characters that are both printable and visible. The following strips the entire non-[:graph:] character set. A simplified version for the more common task of removing only spaces and tabs follows it. |
||
< |
<syntaxhighlight lang="awk">function trimleft(str ,c, out, arr) { |
||
c = split(str, arr, "") |
c = split(str, arr, "") |
||
for ( i = match(str, /[[:graph:]]/); i <= c; i++) |
for ( i = match(str, /[[:graph:]]/); i <= c; i++) |
||
Line 296: | Line 458: | ||
print "right = |" trimright(str) "|" |
print "right = |" trimright(str) "|" |
||
print "both = |" trim(str) "|" |
print "both = |" trim(str) "|" |
||
}</ |
}</syntaxhighlight> |
||
{{out|Output from 26 May 2015}} |
{{out|Output from 26 May 2015}} |
||
Line 316: | Line 478: | ||
Simplified for removing [:blank:] (spaces and tabs) or [:space:] (for [ \t\r\n\v\f] ). This method does not work using [:graph:] |
Simplified for removing [:blank:] (spaces and tabs) or [:space:] (for [ \t\r\n\v\f] ). This method does not work using [:graph:] |
||
< |
<syntaxhighlight lang="awk">function trim(str) { |
||
gsub(/^[[:blank:]]+/,"", str) # Remove leading |
gsub(/^[[:blank:]]+/,"", str) # Remove leading |
||
gsub(/[[:blank:]]+$/,"", str) # Remove trailing |
gsub(/[[:blank:]]+$/,"", str) # Remove trailing |
||
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "", str) # Remove both |
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "", str) # Remove both |
||
return str; |
return str; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|BaCon}}== |
|||
<syntaxhighlight lang="qbasic"> |
|||
'---Remove leading whitespace |
|||
PRINT CHOP$(" String with spaces "," ",1) |
|||
'---Remove trailing whitespace |
|||
PRINT CHOP$(" String with spaces "," ",2) |
|||
'---Remove both leading and trailing whitespace |
|||
PRINT CHOP$(" String with spaces "," ",0) |
|||
</syntaxhighlight> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QBasic}} |
{{works with|QBasic}} |
||
< |
<syntaxhighlight lang="qbasic"> mystring$=ltrim(mystring$) ' remove leading whitespace |
||
mystring$=rtrim(mystring$) ' remove trailing whitespace |
mystring$=rtrim(mystring$) ' remove trailing whitespace |
||
mystring$=ltrim(rtrim(mystring$)) ' remove both leading and trailing whitespace</ |
mystring$=ltrim(rtrim(mystring$)) ' remove both leading and trailing whitespace</syntaxhighlight> |
||
=={{header|BASIC256}}== |
|||
<syntaxhighlight lang="freebasic">s$ = " \tRosetta Code \v\f\r\n" |
|||
print ltrim(s$) # remove leading whitespace |
|||
print rtrim(s$) # remove trailing whitespace |
|||
print trim(s$) # remove both leading and trailing whitespace</syntaxhighlight> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> REM Remove leading whitespace: |
||
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE |
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE |
||
Line 338: | Line 520: | ||
REM Remove both leading and trailing whitespace: |
REM Remove both leading and trailing whitespace: |
||
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE |
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE |
||
WHILE ASC(RIGHT$(A$))<=32 A$ = LEFT$(A$) : ENDWHILE</ |
WHILE ASC(RIGHT$(A$))<=32 A$ = LEFT$(A$) : ENDWHILE</syntaxhighlight> |
||
=={{header|BQN}}== |
|||
The following program uses an AND-scan (a well known idiom for leading characters) to find the leading whitespace. The whitespace characters are then filtered out from those locations. |
|||
Composing the leading and trailing idioms removes whitespace from both sides. |
|||
<syntaxhighlight lang="bqn">ws ← @+⟨9, 10, 11, 12, 13, 32, 133, 160, |
|||
5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, |
|||
8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, |
|||
12288⟩ |
|||
Lead ← (¬·∧`∊⟜ws)⊸/ |
|||
Trail ← (¬·∧`⌾⌽∊⟜ws)⊸/ |
|||
•Show Lead " fs df" |
|||
•Show Trail "fdsf asd " |
|||
•Show Lead∘Trail " white space "</syntaxhighlight><syntaxhighlight lang="text">"fs df" |
|||
"fdsf asd" |
|||
"white space"</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
Greedy pattern matching is not Bracmat's strongest field. So instead of a pattern that globs all white space characters, we have a pattern that finds the first non-whitespace character. That character, and the remainder of the subject string, constitute a left trimmed string. To do a right trim, we reverse the string, do a left trim and reverse back. |
Greedy pattern matching is not Bracmat's strongest field. So instead of a pattern that globs all white space characters, we have a pattern that finds the first non-whitespace character. That character, and the remainder of the subject string, constitute a left trimmed string. To do a right trim, we reverse the string, do a left trim and reverse back. |
||
< |
<syntaxhighlight lang="bracmat">( ( ltrim |
||
= s |
= s |
||
. @( !arg |
. @( !arg |
||
Line 375: | Line 576: | ||
& out$(str$("trim :[" trim$!string "]")) |
& out$(str$("trim :[" trim$!string "]")) |
||
& |
& |
||
);</ |
);</syntaxhighlight> |
||
Output (Notice the effect of the ancient \a (alarm) and \v (vertical tab)): |
Output (Notice the effect of the ancient \a (alarm) and \v (vertical tab)): |
||
<pre>Input:[ Hear |
<pre>Input:[ Hear |
||
Line 396: | Line 597: | ||
=={{header|Burlesque}}== |
=={{header|Burlesque}}== |
||
< |
<syntaxhighlight lang="burlesque"> |
||
blsq ) " this is a string "t[ |
blsq ) " this is a string "t[ |
||
"this is a string " |
"this is a string " |
||
Line 403: | Line 604: | ||
blsq ) " this is a string "tt |
blsq ) " this is a string "tt |
||
"this is a string" |
"this is a string" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 451: | Line 652: | ||
free(d); |
free(d); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header| |
==={{header|Gadget}}=== |
||
<lang cpp>#include <boost/algorithm/string.hpp> |
|||
#include <string> |
|||
#include <iostream> |
|||
<p>Gadget C-library: |
|||
int main( ) { |
|||
[https://github.com/DanielStuardo/Gadget Gadget C-library in Github] |
|||
std::string testphrase( " There are unwanted blanks here! " ) ; |
|||
</p> |
|||
std::string lefttrimmed = boost::trim_left_copy( testphrase ) ; |
|||
<syntaxhighlight lang="c"> |
|||
std::string righttrimmed = boost::trim_right_copy( testphrase ) ; |
|||
#include <gadget/gadget.h> |
|||
std::cout << "The test phrase is :" << testphrase << "\n" ; |
|||
std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ; |
|||
LIB_GADGET_START |
|||
std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ; |
|||
boost::trim( testphrase ) ; |
|||
Main |
|||
std::cout << "Trimmed on both sides :" << testphrase << "\n" ; |
|||
String r,s,t; |
|||
}</lang> |
|||
Stack{ |
|||
Store ( r, Trim_r (Upper(" \n\t este mensaje será modificado " )) ) |
|||
Store ( s, Trim_l (Reverse(Upper(" \n\t este mensaje será odasrever " ))) ) |
|||
Store ( t, Trim (Upper(" \n\t este mensaje será modificado " )) ) |
|||
}Stack_off |
|||
Print "Right Trim = [%s]\nLeft Trim = [%s]\nAll Trim = [%s]\n",r,s,t; |
|||
Free secure r,t, s; |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
|||
<PRE>The test phrase is : There are unwanted blanks here! |
|||
Right Trim = [ |
|||
Trimmed on the left side :There are unwanted blanks here! |
|||
ESTE MENSAJE SERÁ MODIFICADO] |
|||
Trimmed on the right side : There are unwanted blanks here! |
|||
Left Trim = [REVERSADO ÁRES EJASNEM ETSE |
|||
Trimmed on both sides :There are unwanted blanks here!</PRE> |
|||
] |
|||
All Trim = [ESTE MENSAJE SERÁ MODIFICADO] |
|||
</pre> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
public class TrimExample |
public class TrimExample |
||
Line 492: | Line 704: | ||
return "'" + s + "'"; |
return "'" + s + "'"; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 500: | Line 712: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|C++}}== |
||
<syntaxhighlight lang="cpp">#include <boost/algorithm/string.hpp> |
|||
{{works with|GNU Cobol|2.0}} |
|||
#include <string> |
|||
<lang cobol>DISPLAY "'" FUNCTION TRIM(str, LEADING) "'" |
|||
#include <iostream> |
|||
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'" |
|||
DISPLAY "'" FUNCTION TRIM(str) "'"</lang> |
|||
int main( ) { |
|||
{{works with|IBM ILE COBOL}} |
|||
std::string testphrase( " There are unwanted blanks here! " ) ; |
|||
<lang cobol>DISPLAY "'" FUNCTION TRIML(str) "'" |
|||
std::string lefttrimmed = boost::trim_left_copy( testphrase ) ; |
|||
DISPLAY "'" FUNCTION TRIMR(str) "'" |
|||
std::string righttrimmed = boost::trim_right_copy( testphrase ) ; |
|||
DISPLAY "'" FUNCTION TRIM(str) "'"</lang> |
|||
std::cout << "The test phrase is :" << testphrase << "\n" ; |
|||
std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ; |
|||
std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ; |
|||
boost::trim( testphrase ) ; |
|||
std::cout << "Trimmed on both sides :" << testphrase << "\n" ; |
|||
return 0 ; |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<PRE>The test phrase is : There are unwanted blanks here! |
|||
Trimmed on the left side :There are unwanted blanks here! |
|||
Trimmed on the right side : There are unwanted blanks here! |
|||
Trimmed on both sides :There are unwanted blanks here!</PRE> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure"> |
||
(use 'clojure.string) |
(use 'clojure.string) |
||
(triml " my string ") |
(triml " my string ") |
||
Line 520: | Line 743: | ||
(trim " \t\r\n my string \t\r\n ") |
(trim " \t\r\n my string \t\r\n ") |
||
=> "my string" |
=> "my string" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|COBOL}}== |
|||
{{works with|GNU Cobol|2.0}} |
|||
<syntaxhighlight lang="cobol">DISPLAY "'" FUNCTION TRIM(str, LEADING) "'" |
|||
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'" |
|||
DISPLAY "'" FUNCTION TRIM(str) "'"</syntaxhighlight> |
|||
{{works with|IBM ILE COBOL}} |
|||
<syntaxhighlight lang="cobol">DISPLAY "'" FUNCTION TRIML(str) "'" |
|||
DISPLAY "'" FUNCTION TRIMR(str) "'" |
|||
DISPLAY "'" FUNCTION TRIM(str) "'"</syntaxhighlight> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">; Common whitespace characters |
||
(defvar *whitespace* '(#\Space #\Newline #\Tab)) |
(defvar *whitespace* '(#\Space #\Newline #\Tab)) |
||
Line 558: | Line 792: | ||
(string-right-trim *unicode-whitespace* unicode-str) |
(string-right-trim *unicode-whitespace* unicode-str) |
||
; -> " foo bar baz"</ |
; -> " foo bar baz"</syntaxhighlight> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
<syntaxhighlight lang="ruby"> |
|||
<lang Ruby> |
|||
def strip_whitepace(s) |
def strip_whitepace(s) |
||
puts s.lstrip() |
puts s.lstrip() |
||
Line 572: | Line 806: | ||
# => hello |
# => hello |
||
# => hello |
# => hello |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string; |
||
void main() { |
void main() { |
||
Line 582: | Line 816: | ||
assert(s.stripRight() == " \t \r \n String with spaces"); |
assert(s.stripRight() == " \t \r \n String with spaces"); |
||
assert(s.strip() == "String with spaces"); |
assert(s.strip() == "String with spaces"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}/{{header|Pascal}}== |
=={{header|Delphi}}/{{header|Pascal}}== |
||
< |
<syntaxhighlight lang="delphi">program StripWhitespace; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 598: | Line 832: | ||
Writeln('"' + TrimRight(TEST_STRING) + '"'); |
Writeln('"' + TrimRight(TEST_STRING) + '"'); |
||
Writeln('"' + Trim(TEST_STRING) + '"'); |
Writeln('"' + Trim(TEST_STRING) + '"'); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
{{Trans|Delphi}} |
{{Trans|Delphi}} |
||
< |
<syntaxhighlight lang="delphi">const TEST_STRING = ' String with spaces '; |
||
PrintLn('"' + TEST_STRING + '"'); |
PrintLn('"' + TEST_STRING + '"'); |
||
PrintLn('"' + TrimLeft(TEST_STRING) + '"'); |
PrintLn('"' + TrimLeft(TEST_STRING) + '"'); |
||
PrintLn('"' + TrimRight(TEST_STRING) + '"'); |
PrintLn('"' + TrimRight(TEST_STRING) + '"'); |
||
PrintLn('"' + Trim(TEST_STRING) + '"');</ |
PrintLn('"' + Trim(TEST_STRING) + '"');</syntaxhighlight> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang=easylang> |
|||
func iswhite c$ . |
|||
if c$ = " " or c$ = "\t" or c$ = "\n" |
|||
return 1 |
|||
. |
|||
. |
|||
func$ strip s$ top tail . |
|||
a = 1 |
|||
if top = 1 |
|||
repeat |
|||
c$ = substr s$ a 1 |
|||
until iswhite c$ = 0 |
|||
a += 1 |
|||
. |
|||
. |
|||
b = len s$ |
|||
if tail = 1 |
|||
repeat |
|||
c$ = substr s$ b 1 |
|||
until iswhite c$ = 0 |
|||
b -= 1 |
|||
. |
|||
. |
|||
return substr s$ a (b - a + 1) |
|||
. |
|||
print strip " Hello world " 1 1 & "." |
|||
print strip " Hello world " 0 1 & "." |
|||
print strip " Hello world " 1 1 & "." |
|||
</syntaxhighlight> |
|||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(define witt |
(define witt |
||
" The limits of my world are the limits of my langage. ") |
" The limits of my world are the limits of my langage. ") |
||
Line 619: | Line 885: | ||
(string->html (string-trim-right witt)) |
(string->html (string-trim-right witt)) |
||
→ " The limits of my world are the limits of my langage." |
→ " The limits of my world are the limits of my langage." |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elena}}== |
|||
ELENA 3.2.1 : |
|||
<lang elena>import extensions. |
|||
=={{header|Elena}}== |
|||
program = |
|||
ELENA 4.x : |
|||
[ |
|||
<syntaxhighlight lang="elena">import extensions; |
|||
var toTrim := " Trim me ". |
|||
console printLine("'", toTrim trimLeft,"'"). |
|||
public program() |
|||
console printLine("'", toTrim trimRight,"'"). |
|||
{ |
|||
console printLine("'", toTrim trim,"'"). |
|||
var toTrim := " Trim me "; |
|||
].</lang> |
|||
console.printLine("'", toTrim.trimLeft(),"'"); |
|||
console.printLine("'", toTrim.trimRight(),"'"); |
|||
console.printLine("'", toTrim.trim(),"'"); |
|||
}</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 639: | Line 906: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">str = "\n \t foo \n\t bar \t \n" |
||
IO.inspect String.strip(str) |
IO.inspect String.strip(str) |
||
IO.inspect String.rstrip(str) |
IO.inspect String.rstrip(str) |
||
IO.inspect String.lstrip(str)</ |
IO.inspect String.lstrip(str)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 651: | Line 918: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
===trim left=== |
|||
<lang Emacs Lisp> |
|||
(defun trim-l (str) |
|||
(replace-regexp-in-string "^ +" "" str) ) |
|||
As of Emacs 24.4, the subr-x.el library contains string trimming functions: |
|||
(setq str " left between right ") |
|||
(insert (trim-l str) ) |
|||
</lang> |
|||
<b>Output:</b> |
|||
<pre> |
|||
left between right |
|||
</pre> |
|||
<syntaxhighlight lang="lisp">(string-trim-left " left center right ") ;=> "left center right " |
|||
===trim right=== |
|||
(string-trim-right " left center right ") ;=> " left center right" |
|||
<lang Emacs Lisp> |
|||
(string-trim " left center right ") ;=> "left center right"</syntaxhighlight> |
|||
(defun trim-r (str) |
|||
(replace-regexp-in-string " +$" "" str) ) |
|||
This can be emulated by using regex replacement: |
|||
(setq str " left between right ") |
|||
(insert (trim-r str) ) |
|||
</lang> |
|||
<b>Output:</b> |
|||
<pre> |
|||
left between right |
|||
</pre> |
|||
===trim version 1=== |
|||
<lang Emacs Lisp> |
|||
(defun trim (str) |
|||
(trim-l (trim-r str) )) |
|||
<syntaxhighlight lang="lisp">(defun string-trim-left (str) |
|||
(setq str " left between right ") |
|||
(replace-regexp-in-string "^[ \t\r\n]*" "" str)) |
|||
(insert (trim str) ) |
|||
</lang> |
|||
<b>Output:</b> |
|||
<pre> |
|||
left between right |
|||
</pre> |
|||
===trim version 2=== |
|||
<lang Emacs Lisp> |
|||
(defun trim (str) |
|||
(mapconcat 'identity (split-string str) " ") ) |
|||
(defun string-trim-right (str) |
|||
(setq str " left between right ") |
|||
(replace-regexp-in-string "[ \t\r\n]*$" "" str)) |
|||
(insert (trim str) ) |
|||
</lang> |
|||
(defun string-trim (str) |
|||
<b>Output:</b> |
|||
(string-trim-left (string-trim-right str)))</syntaxhighlight> |
|||
<pre> |
|||
left between right |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel |
||
1> string:strip(" Hello World! ", left). %remove leading whitespaces |
1> string:strip(" Hello World! ", left). %remove leading whitespaces |
||
"Hello World! " |
"Hello World! " |
||
Line 712: | Line 946: | ||
3> string:strip(" Hello World! ", both). % remove both leading and trailing whitespace |
3> string:strip(" Hello World! ", both). % remove both leading and trailing whitespace |
||
"Hello World!" |
"Hello World!" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Line 722: | Line 956: | ||
using escaped hexadecimal , example : \x04 to represent hexadecimal ascii 04 or \u2A7C for 4-digit UTF, or more than two digit ascii characters : \u0127. |
using escaped hexadecimal , example : \x04 to represent hexadecimal ascii 04 or \u2A7C for 4-digit UTF, or more than two digit ascii characters : \u0127. |
||
< |
<syntaxhighlight lang="euphoria">include std/console.e |
||
include std/text.e |
include std/text.e |
||
Line 738: | Line 972: | ||
end for |
end for |
||
any_key()</ |
any_key()</syntaxhighlight> |
||
Output:<pre> |
Output:<pre> |
||
String Trimmed is now: A B C |
String Trimmed is now: A B C |
||
Line 750: | Line 984: | ||
Press Any Key to continue... |
Press Any Key to continue... |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp">[<EntryPoint>] |
||
let main args = |
let main args = |
||
printfn "%A" (args.[0].TrimStart()) |
printfn "%A" (args.[0].TrimStart()) |
||
printfn "%A" (args.[0].TrimEnd()) |
printfn "%A" (args.[0].TrimEnd()) |
||
printfn "%A" (args.[0].Trim()) |
printfn "%A" (args.[0].Trim()) |
||
0</ |
0</syntaxhighlight> |
||
<pre> |
<pre> |
||
>rosetta " a string " |
>rosetta " a string " |
||
Line 766: | Line 999: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.98}} |
|||
<lang factor> |
|||
<syntaxhighlight lang="factor">USE: unicode |
|||
USING: unicode.categories; |
|||
" test string " [ blank? ] trim ! leading and trailing |
" test string " [ blank? ] trim ! leading and trailing |
||
" test string " [ blank? ] trim-head ! only leading |
" test string " [ blank? ] trim-head ! only leading |
||
" test string " [ blank? ] trim-tail ! only trailing |
" test string " [ blank? ] trim-tail ! only trailing</syntaxhighlight> |
||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 779: | Line 1,010: | ||
After the string processing is completed on the stack the program can print or copy back to memory as required. |
After the string processing is completed on the stack the program can print or copy back to memory as required. |
||
< |
<syntaxhighlight lang="forth">: -leading ( addr len -- addr' len' ) \ called "minus-leading" |
||
begin |
begin |
||
over c@ bl = \ fetch character at addr, test if blank (space) |
over c@ bl = \ fetch character at addr, test if blank (space) |
||
while |
while |
||
\ |
\ cut 1 leading character by incrementing address & decrementing length |
||
1 /string \ "cut-string" |
1 /string \ "cut-string" |
||
repeat ;</ |
repeat ;</syntaxhighlight> |
||
The counterpart function "-trailing" is normally part of a standard forth system |
The counterpart function "-trailing" is normally part of a standard forth system, |
||
so by concatenating both we create a "STRIP" function. |
|||
<lang> : |
<syntaxhighlight lang="text"> : strip ( addr len -- addr' len') -leading -trailing ; </syntaxhighlight> |
||
Test at the Forth console |
Test at the Forth console |
||
<pre>s" Trim this string. " |
<pre>: mystring s" Trim this string. " ; |
||
Trim this string. ok</pre> |
|||
mstring type Trim this string. ok |
|||
mstring -leading type Trim this string. ok |
|||
mystring -trailing type Trim this string. ok |
|||
mytring strip type Trim this string. ok</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 801: | Line 1,036: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Const whitespace = !" \t\n\v\f\r" |
Const whitespace = !" \t\n\v\f\r" |
||
Line 824: | Line 1,059: | ||
Print "Right trimmed" , "=> Length = "; Len(s2) |
Print "Right trimmed" , "=> Length = "; Len(s2) |
||
Print "Fully trimmed" , "=> Length = "; Len(s3) |
Print "Fully trimmed" , "=> Length = "; Len(s3) |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 839: | Line 1,074: | ||
Right trimmed => Length = 15 |
Right trimmed => Length = 15 |
||
Fully trimmed => Length = 12 |
Fully trimmed => Length = 12 |
||
</pre> |
|||
=={{header|FutureBasic}}== |
|||
<syntaxhighlight lang="futurebasic">window 1 |
|||
CFCharacterSetRef set, invSet |
|||
CFStringRef s, s1, s2, s3 |
|||
NSUInteger index |
|||
CFRange range |
|||
set = fn CharacterSetWhitespaceAndNewlineSet |
|||
invSet = fn CharacterSetInvertedSet( set ) |
|||
text ,,,,, 200 |
|||
s = @" a string "// 5 leading spaces, 8 trailing spaces |
|||
print s, len(s)@" chars" |
|||
// left trim |
|||
index = 0 |
|||
range = fn StringRangeOfCharacterFromSet( s, invSet ) |
|||
if ( range.location != NSNotFound ) then index = range.location |
|||
s1 = fn StringSubstringFromIndex( s, index ) |
|||
print s1, len(s1)@" chars" |
|||
// right trim |
|||
index = len(s) |
|||
range = fn StringRangeOfCharacterFromSetWithOptions( s, invSet, NSBackwardsSearch ) |
|||
if ( range.location != NSNotFound ) then index = range.location + 1 |
|||
s2 = fn StringSubstringToIndex( s, index ) |
|||
print s2, len(s2)@" chars" |
|||
// trim |
|||
s3 = fn StringByTrimmingCharactersInSet( s, set ) |
|||
print s3, len(s3)@" chars" |
|||
HandleEvents</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
a string 21 chars |
|||
a string 16 chars |
|||
a string 13 chars |
|||
a string 8 chars |
|||
</pre> |
</pre> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=62e56fea0f74819daa3d3a548869fa90 Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=62e56fea0f74819daa3d3a548869fa90 Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sString As String = " Hello world! " |
Dim sString As String = " Hello world! " |
||
Line 851: | Line 1,129: | ||
Print Trim(sString) & "\t\tString length = " & Len(Trim(sString)) & " - String with both leading and trailing whitespace removed" |
Print Trim(sString) & "\t\tString length = " & Len(Trim(sString)) & " - String with both leading and trailing whitespace removed" |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 861: | Line 1,139: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 882: | Line 1,160: | ||
func show(label, str string) { |
func show(label, str string) { |
||
fmt.Printf("%s: |%s| %v\n", label, str, []rune(str)) |
fmt.Printf("%s: |%s| %v\n", label, str, []rune(str)) |
||
}</ |
}</syntaxhighlight> |
||
Example text is shows a leading linefeed and tab, and three trailing spaces. The code uses the Unicode definition of whitespace. Other defintions could be implemented with a custom function given to TrimXFunc. |
Example text is shows a leading linefeed and tab, and three trailing spaces. The code uses the Unicode definition of whitespace. Other defintions could be implemented with a custom function given to TrimXFunc. |
||
Line 897: | Line 1,175: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution uses StringUtils class from [http://commons.apache.org/lang/ Apache Commons "Lang" library]: |
Solution uses StringUtils class from [http://commons.apache.org/lang/ Apache Commons "Lang" library]: |
||
< |
<syntaxhighlight lang="groovy">//Grape setup to get library |
||
@Grab('org.apache.commons:commons-lang3:3.0.1') |
@Grab('org.apache.commons:commons-lang3:3.0.1') |
||
import static org.apache.commons.lang3.StringUtils.* |
import static org.apache.commons.lang3.StringUtils.* |
||
Line 918: | Line 1,196: | ||
println '============\n\nRight Stripped\n------------' |
println '============\n\nRight Stripped\n------------' |
||
printTest stripEnd(abc, null) |
printTest stripEnd(abc, null) |
||
println '============'</ |
println '============'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Unstripped |
<pre>Unstripped |
||
Line 945: | Line 1,223: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Char (isSpace) |
||
import Data.List (dropWhileEnd) |
|||
trimLeft :: String -> String |
trimLeft :: String -> String |
||
Line 951: | Line 1,230: | ||
trimRight :: String -> String |
trimRight :: String -> String |
||
trimRight |
trimRight = dropWhileEnd isSpace |
||
trimRight (c : cs) = c : trimRight cs |
|||
trim :: String -> String |
trim :: String -> String |
||
trim = trimLeft . trimRight</ |
trim = trimLeft . trimRight</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
This solution takes the phrase "other such characters that have no corresponding graphical representation" quite literallly. |
This solution takes the phrase "other such characters that have no corresponding graphical representation" quite literallly. |
||
< |
<syntaxhighlight lang="unicon">procedure main() |
||
unp := &cset[1+:32]++' \t'++&cset[127:0] # all 'unprintable' chars |
unp := &cset[1+:32]++' \t'++&cset[127:0] # all 'unprintable' chars |
||
s := " Hello, people of earth! " |
s := " Hello, people of earth! " |
||
Line 966: | Line 1,244: | ||
write("trailing trim: '",trim(s,unp),"'") |
write("trailing trim: '",trim(s,unp),"'") |
||
write("full trim: '",reverse(trim(reverse(trim(s,unp)),unp)),"'") |
write("full trim: '",reverse(trim(reverse(trim(s,unp)),unp)),"'") |
||
end</ |
end</syntaxhighlight> |
||
{{out|Sample run}} |
{{out|Sample run}} |
||
<pre>->trim |
<pre>->trim |
||
Line 977: | Line 1,255: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Note: The <code>quote</code> verb is only used to enclose the resulting string in single quotes so the beginning and end of the new string are visible. |
Note: The <code>quote</code> verb is only used to enclose the resulting string in single quotes so the beginning and end of the new string are visible. |
||
< |
<syntaxhighlight lang="j"> require 'strings' NB. the strings library is automatically loaded in versions from J7 on |
||
quote dlb ' String with spaces ' NB. delete leading blanks |
quote dlb ' String with spaces ' NB. delete leading blanks |
||
'String with spaces ' |
'String with spaces ' |
||
Line 983: | Line 1,261: | ||
' String with spaces' |
' String with spaces' |
||
quote dltb ' String with spaces ' NB. delete leading and trailing blanks |
quote dltb ' String with spaces ' NB. delete leading and trailing blanks |
||
'String with spaces'</ |
'String with spaces'</syntaxhighlight> |
||
In addition <code>deb</code> (delete extraneous blanks) will trim both leading and trailing blanks as well as replace consecutive spaces within the string with a single space. |
In addition <code>deb</code> (delete extraneous blanks) will trim both leading and trailing blanks as well as replace consecutive spaces within the string with a single space. |
||
< |
<syntaxhighlight lang="j"> quote deb ' String with spaces ' NB. delete extraneous blanks |
||
'String with spaces'</ |
'String with spaces'</syntaxhighlight> |
||
These existing definitions can be easily amended to include whitespace other than spaces if desired. |
These existing definitions can be easily amended to include whitespace other than spaces if desired. |
||
< |
<syntaxhighlight lang="j">whpsc=: ' ',TAB NB. define whitespace as desired |
||
dlws=: }.~ (e.&whpsc i. 0:) NB. delete leading whitespace (spaces and tabs) |
dlws=: }.~ (e.&whpsc i. 0:) NB. delete leading whitespace (spaces and tabs) |
||
dtws=: #~ ([: +./\. -.@:e.&whpsc) NB. delete trailing whitespace |
dtws=: #~ ([: +./\. -.@:e.&whpsc) NB. delete trailing whitespace |
||
dltws=: #~ ([: (+./\ *. +./\.) -.@:e.&whpsc) NB. delete leading & trailing whitespace |
dltws=: #~ ([: (+./\ *. +./\.) -.@:e.&whpsc) NB. delete leading & trailing whitespace |
||
dews=: #~ (+. (1: |. (> </\)))@(-.@:e.&whpsc) NB. delete extraneous whitespace</ |
dews=: #~ (+. (1: |. (> </\)))@(-.@:e.&whpsc) NB. delete extraneous whitespace</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Java has the ''trim'' and ''strip'' operations. Both will achieve a similar result.<br /> |
|||
The <code>trim</code> method works on Unicode values under <kbd>0x20</kbd>. Whereas, ''strip'' will remove all Unicode white-space characters. |
|||
<syntaxhighlight lang="java"> |
|||
" abc".stripLeading() |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
"abc ".stripTrailing() |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
" abc ".strip() |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
" abc ".trim() |
|||
</syntaxhighlight> |
|||
For more control over what characters should be removed, you could implement your own methods. |
|||
<syntaxhighlight lang="java"> |
|||
String removeLeading(String string, char[] characters) { |
|||
int index = 0; |
|||
for (char characterA : string.toCharArray()) { |
|||
for (char characterB : characters) { |
|||
if (characterA != characterB) |
|||
return string.substring(index); |
|||
} |
|||
index++; |
|||
} |
|||
return string; |
|||
} |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="java"> |
|||
String removeTrailing(String string, char[] characters) { |
|||
for (int index = string.length() - 1; index >= 0; index--) { |
|||
for (char character : characters) { |
|||
if (string.charAt(index) != character) |
|||
return string.substring(0, index + 1); |
|||
} |
|||
} |
|||
return string; |
|||
} |
|||
</syntaxhighlight> |
|||
<br /> |
|||
An alternate demonstration<br /> |
|||
Java offers <code>String.trim</code>. However, this function only strips out ASCII control characters. As such it should generally be avoided for processing text. |
|||
Rather, the function <code>Character.isWhitespace</code> should be used to strip out any Unicode whitespaces. Note that the Java implementation slightly deviates from the strict Unicode definition: For example, non-breaking spaces are not considered whitespace by Java. This is fine for stripping leading/trailing whitespaces, as a non-breaking space generally should not appear there, but is something to keep in mind. |
|||
Left trim and right trim taken from [http://www.fromdev.com/2009/07/playing-with-java-string-trim-basics.html here]. |
Left trim and right trim taken from [http://www.fromdev.com/2009/07/playing-with-java-string-trim-basics.html here]. |
||
<code>Character.isWhitespace()</code> returns true if the character given is one of the following [[Unicode]] characters: '\u00A0', '\u2007', '\u202F', '\u0009', '\u000A', '\u000B', '\u000C', '\u000D', '\u001C', '\u001D', '\u001E', or '\u001F'. |
|||
<lang java> |
|||
public class Trims{ |
|||
public static String ltrim(String s){ |
|||
int i = 0; |
|||
while (i < s.length() && Character.isWhitespace(s.charAt(i))){ |
|||
i++; |
|||
} |
|||
return s.substring(i); |
|||
} |
|||
<syntaxhighlight lang="java">public class Trims{ |
|||
public static String rtrim(String s){ |
|||
public static String ltrim(String s) { |
|||
int i = 0; |
|||
while (i > 0 && Character.isWhitespace(s.charAt(i))){ |
|||
while (i < s.length() && Character.isWhitespace(s.charAt(i))) { |
|||
i--; |
|||
i++; |
|||
} |
|||
return s.substring(i); |
|||
} |
|||
} |
|||
public static |
public static String rtrim(String s) { |
||
int i = s.length() - 1; |
|||
while (i > 0 && Character.isWhitespace(s.charAt(i))) { |
|||
System.out.println(ltrim(s)); |
|||
i--; |
|||
System.out.println(rtrim(s)); |
|||
} |
|||
System.out.println(s.trim()); //trims both ends |
|||
return s.substring(0, i + 1); |
|||
} |
|||
} |
|||
}</lang> |
|||
public static String trim(String s) { |
|||
return rtrim(ltrim(s)); |
|||
} |
|||
public static void main(String[] args) { |
|||
String s = " \t \r \n String with spaces \u2009 \t \r \n "; |
|||
System.out.printf("[%s]\n", ltrim(s)); |
|||
System.out.printf("[%s]\n", rtrim(s)); |
|||
System.out.printf("[%s]\n", trim(s)); |
|||
} |
|||
}</syntaxhighlight> |
|||
===Supplementary-correct version=== |
|||
Unicode *happens* to not have any whitespace characters outside of Basic Multilingual Plane (aka, they all fit inside a <code>char</code>). However, this not something you should ''generally'' rely on, and should assume your strings contain characters in the Supplementary Planes. As such, instead of iterating using <code>String.charAt</code>, prefer instead using <code>String.codePointAt</code> and <code>String.codePointBefore</code>, iterating over actual Unicode Code Points: |
|||
<syntaxhighlight lang="java"> public static String ltrim(String s) { |
|||
int offset = 0; |
|||
while (offset < s.length()) { |
|||
int codePoint = s.codePointAt(offset); |
|||
if (!Character.isWhitespace(codePoint)) break; |
|||
offset += Character.charCount(codePoint); |
|||
} |
|||
return s.substring(offset); |
|||
} |
|||
public static String rtrim(String s) { |
|||
int offset = s.length(); |
|||
while (offset > 0) { |
|||
int codePoint = s.codePointBefore(offset); |
|||
if (!Character.isWhitespace(codePoint)) break; |
|||
offset -= Character.charCount(codePoint); |
|||
} |
|||
return s.substring(0, offset); |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Javascript}}== |
=={{header|Javascript}}== |
||
{{works with|Node.js}} |
{{works with|Node.js}} |
||
{{works with|ECMAScript standard|2015}} |
{{works with|ECMAScript standard|2015}} |
||
< |
<syntaxhighlight lang="javascript">{ |
||
let s = " \t String with spaces \t "; |
let s = " \t String with spaces \t "; |
||
// a future version of ECMAScript will have trimStart(). Some current |
// a future version of ECMAScript will have trimStart(). Some current |
||
Line 1,036: | Line 1,386: | ||
console.log("trimmed right: '" + s.replace(/\s+$/,'') + "'"); |
console.log("trimmed right: '" + s.replace(/\s+$/,'') + "'"); |
||
console.log("trimmed both: '" + s.trim() + "'"); |
console.log("trimmed both: '" + s.trim() + "'"); |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>original: ' String with spaces ' |
<pre>original: ' String with spaces ' |
||
Line 1,047: | Line 1,397: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,095: | Line 1,445: | ||
})(); |
})(); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 1,111: | Line 1,461: | ||
Notice that since jq strings are JSON strings, one must, for example, write "\\s" for the regex '\s'. |
Notice that since jq strings are JSON strings, one must, for example, write "\\s" for the regex '\s'. |
||
< |
<syntaxhighlight lang="jq">def lstrip: sub( "^[\\s\\p{Cc}]+"; "" ); |
||
def rstrip: sub( "[\\s\\p{Cc}]+$"; "" ); |
def rstrip: sub( "[\\s\\p{Cc}]+$"; "" ); |
||
def strip: lstrip | rstrip;</ |
def strip: lstrip | rstrip;</syntaxhighlight> |
||
'''Examples''': |
'''Examples''': |
||
< |
<syntaxhighlight lang="jq">def demo: |
||
"lstrip: \(lstrip)", |
"lstrip: \(lstrip)", |
||
"rstrip: \(rstrip)", |
"rstrip: \(rstrip)", |
||
Line 1,126: | Line 1,476: | ||
"\u0001 \u0002 <- ^A ^B" |
"\u0001 \u0002 <- ^A ^B" |
||
) | demo |
) | demo |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -n -f Strip_whitespace_top_tail.jq |
||
"lstrip: String with spaces \t \r \n " |
"lstrip: String with spaces \t \r \n " |
||
"rstrip: \t \r \n String with spaces" |
"rstrip: \t \r \n String with spaces" |
||
Line 1,136: | Line 1,486: | ||
"lstrip: <- ^A ^B" |
"lstrip: <- ^A ^B" |
||
"rstrip: \u0001 \u0002 <- ^A ^B" |
"rstrip: \u0001 \u0002 <- ^A ^B" |
||
"strip: <- ^A ^B"</ |
"strip: <- ^A ^B"</syntaxhighlight> |
||
=={{header|Jsish}}== |
|||
Using echo-mode unitTest lines. Lines starting with semicolon are echoed and captured during test. |
|||
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish |
|||
/* Strip whitespace from string, in Jsi */ |
|||
var str = ' \n \t String with whitespace \t \n '; |
|||
;'Original'; |
|||
;str; |
|||
;'Default trim characters are space, tab, newline, carriage return'; |
|||
;'trimLeft, remove leading characters'; |
|||
;str.trimLeft(); |
|||
;'trimRight, remove trailing characters'; |
|||
;str.trimRight(); |
|||
;'trim, removes leading and trailing'; |
|||
;str.trim(); |
|||
/* |
|||
=!EXPECTSTART!= |
|||
'Original' |
|||
str ==> |
|||
String with whitespace |
|||
'Default trim characters are space, tab, newline, carriage return' |
|||
'trimLeft, remove leading characters' |
|||
str.trimLeft() ==> String with whitespace |
|||
'trimRight, remove trailing characters' |
|||
str.trimRight() ==> |
|||
String with whitespace |
|||
'trim, removes leading and trailing' |
|||
str.trim() ==> String with whitespace |
|||
=!EXPECTEND!= |
|||
*/</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ jsish -u stripWhitespace.jsi |
|||
[PASS] stripWhitespace.jsi |
|||
prompt$ jsish --U stripWhitespace.jsi |
|||
'Original' |
|||
str ==> |
|||
String with whitespace |
|||
'Default trim characters are space, tab, newline, carriage return' |
|||
'trimLeft, remove leading characters' |
|||
str.trimLeft() ==> String with whitespace |
|||
'trimRight, remove trailing characters' |
|||
str.trimRight() ==> |
|||
String with whitespace |
|||
'trim, removes leading and trailing' |
|||
str.trim() ==> String with whitespace</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="julia">julia> s = " \t \r \n String with spaces \t \r \n " |
||
" \t \r \n String with spaces \t \r \n " |
" \t \r \n String with spaces \t \r \n " |
||
Line 1,150: | Line 1,557: | ||
julia> strip(s) |
julia> strip(s) |
||
"String with spaces"</ |
"String with spaces"</syntaxhighlight> |
||
=={{header|K}}== |
|||
<syntaxhighlight lang=K>dlb: {((" "<x)?1)_x} |
|||
dtb: {(-(|" "<x)?1)_x} |
|||
dlb " this is a test " |
|||
"this is a test " |
|||
dtb " this is a test " |
|||
" this is a test" |
|||
dtb dlb " this is a test " |
|||
"this is a test" |
|||
dtb dlb "this is a test" |
|||
"this is a test"</syntaxhighlight> |
|||
tested with ngn/k |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
It is worth pointing out that Kotlin, unlike Java, has <code>String.trimStart</code> and <code>String.trimEnd</code>. More importantly though, Kotlin’s <code>String.trim</code> actually trims whitespace as defined by Unicode, whereas Java’s just strips Unicode control characters. |
|||
<lang scala>// version 1.0.6 |
|||
Kotlin’s <code>trim</code> family of functions also optionally accept a predicate, which can further customize the stripping behavior, if Unicode whitespace is not what is desired. |
|||
fun main(args: Array<String>) { |
|||
val s = " \tRosetta Code \r\n" |
|||
<syntaxhighlight lang="kotlin">fun main(args: Array<String>) { |
|||
println("Untrimmed => $s") |
|||
val s = " \tRosetta Code \r \u2009 \n" |
|||
println("Left Trimmed => ${s.trimStart()}") |
|||
println(" |
println("Untrimmed => [$s]") |
||
println(" |
println("Left Trimmed => [${s.trimStart()}]") |
||
println("Right Trimmed => [${s.trimEnd()}]") |
|||
}</lang> |
|||
println("Fully Trimmed => [${s.trim()}]") |
|||
}</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Untrimmed => |
Untrimmed => [ Rosetta Code |
||
] |
|||
Left Trimmed => [Rosetta Code |
|||
] |
|||
Right Trimmed => [ Rosetta Code] |
|||
Fully Trimmed => [Rosetta Code] |
|||
</pre> |
|||
=={{header|Ksh}}== |
|||
Left Trimmed => Rosetta Code |
|||
<syntaxhighlight lang="ksh"> |
|||
#!/bin/ksh |
|||
# Strip whitespace from a string/Top and tail |
|||
Right Trimmed => Rosetta Code |
|||
Fully Trimmed => Rosetta Code |
|||
# # Variables: |
|||
</pre> |
|||
# |
|||
str=${1:-" This is a test. "} |
|||
# # Functions: |
|||
# |
|||
# # Function _striphead(str) - Return str with leading while space stripped |
|||
# |
|||
function _striphead { |
|||
typeset _str ; _str="$1" |
|||
echo "${_str##*(\s)}" |
|||
} |
|||
# # Function _striptail(str) - Return str with trailing while space stripped |
|||
# |
|||
function _striptail { |
|||
typeset _str ; _str="$1" |
|||
while [[ ${_str} != ${_str%+(\s)} ]]; do |
|||
_str=${_str%+(\s)} |
|||
done |
|||
echo "${_str}" |
|||
} |
|||
###### |
|||
# main # |
|||
###### |
|||
printf ">%s< (Unstripped %d chars)\n" "${str}" ${#str} |
|||
sstr=$(_striphead "${str}") |
|||
printf ">%s< (Strip leading (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} )) |
|||
sstr=$(_striptail "${str}") |
|||
printf ">%s< (Strip trailing (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} )) |
|||
sstr=$(_striphead "$(_striptail "${str}")") |
|||
printf ">%s< (Strip both (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} ))</syntaxhighlight> |
|||
{{out}}<pre>> This is a test. < (Unstripped 19 chars) |
|||
>This is a test. < (Strip leading (1 chars)) |
|||
> This is a test.< (Strip trailing (3 chars)) |
|||
>This is a test.< (Strip both (4 chars))</pre> |
|||
=={{header|Lambdatalk}}== |
|||
<syntaxhighlight lang="scheme"> |
|||
initial string : {def string ___String with "_" displaying space_____} |
|||
-> string ___String with "_" displaying space_____ |
|||
trimLeft : {S.replace ^_+ by in {string}} |
|||
-> String with "_" displaying space_____ |
|||
trimRight : {S.replace _+$ by in {string}} |
|||
-> ___String with "_" displaying space |
|||
trimBoth : {S.replace _+$ by in {S.replace ^_+ by in {string}}} |
|||
-> String with "_" displaying space |
|||
</syntaxhighlight> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
Line 1,177: | Line 1,665: | ||
The '>' and '<' strings have been included in this example to demonstrate the whitespace trim. |
The '>' and '<' strings have been included in this example to demonstrate the whitespace trim. |
||
< |
<syntaxhighlight lang="lasso">// String with leading whitespace removed |
||
'>' + (' \t Hello')->trim& + '<' |
'>' + (' \t Hello')->trim& + '<' |
||
Line 1,184: | Line 1,672: | ||
// String with both leading and trailing whitespace removed |
// String with both leading and trailing whitespace removed |
||
'>' + (' \t Hello \t ')->trim& + '<'</ |
'>' + (' \t Hello \t ')->trim& + '<'</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">a$=" This is a test " |
||
'LB TRIM$ removes characters with codes 0..31 as well as a space(code 32) |
'LB TRIM$ removes characters with codes 0..31 as well as a space(code 32) |
||
Line 1,214: | Line 1,702: | ||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
Using atoms for representing strings and assuming an ASCII text encoding: |
Using atoms for representing strings and assuming an ASCII text encoding: |
||
< |
<syntaxhighlight lang="logtalk"> |
||
:- object(whitespace). |
:- object(whitespace). |
||
Line 1,248: | Line 1,736: | ||
:- end_object. |
:- end_object. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Sample output: |
Sample output: |
||
< |
<syntaxhighlight lang="text"> |
||
| ?- whitespace::trim('\n\t Rosetta Code \t\n', TrimLeft, TrimRight, TrimBoth). |
| ?- whitespace::trim('\n\t Rosetta Code \t\n', TrimLeft, TrimRight, TrimBoth). |
||
TrimLeft = 'Rosetta Code \t\n', |
TrimLeft = 'Rosetta Code \t\n', |
||
Line 1,256: | Line 1,744: | ||
TrimBoth = 'Rosetta Code' |
TrimBoth = 'Rosetta Code' |
||
yes |
yes |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">str = " \t \r \n String with spaces \t \r \n " |
||
print( string.format( "Leading whitespace removed: %s", str:match( "^%s*(.+)" ) ) ) |
print( string.format( "Leading whitespace removed: %s", str:match( "^%s*(.+)" ) ) ) |
||
print( string.format( "Trailing whitespace removed: %s", str:match( "(.-)%s*$" ) ) ) |
print( string.format( "Trailing whitespace removed: %s", str:match( "(.-)%s*$" ) ) ) |
||
print( string.format( "Leading and trailing whitespace removed: %s", str:match( "^%s*(.-)%s*$" ) ) )</ |
print( string.format( "Leading and trailing whitespace removed: %s", str:match( "^%s*(.-)%s*$" ) ) )</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Module CheckIt { |
|||
filter$=chr$(0) |
|||
for i=1 to 31:filter$+=chr$(i):next |
|||
a$=chr$(9)+" There are unwanted blanks here! "+chr$(9) |
|||
a$=filter$(a$,filter$) ' exclude non printable characters |
|||
\\ string encoded as UTF16LE |
|||
Print Len(a$)=39 |
|||
Print(ltrim$(a$)) |
|||
Print(rtrim$(a$)) |
|||
Print(trim$(a$)) |
|||
\\ string encoded as ANSI base to Locale |
|||
oldlocale=Locale |
|||
Locale 1033 |
|||
a$=str$(a$) |
|||
Print Len(a$)=19.5 ' unit for length is a word (2 bytes), so 19.5 means 19.5*2 bytes= 39 bytes |
|||
PrintAnsi(ltrim$(a$ as byte)) |
|||
PrintAnsi(rtrim$(a$ as byte)) |
|||
PrintAnsi(trim$(a$ as byte)) |
|||
Locale oldlocale |
|||
Sub Print(a$) |
|||
Print "*"+a$+"*" |
|||
End Sub |
|||
Sub PrintAnsi(a$) |
|||
Print "*"+chr$(a$)+"*" |
|||
End Sub |
|||
} |
|||
Checkit |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
True |
|||
*There are unwanted blanks here! * |
|||
* There are unwanted blanks here!* |
|||
*There are unwanted blanks here!* |
|||
True |
|||
*There are unwanted blanks here! * |
|||
* There are unwanted blanks here!* |
|||
*There are unwanted blanks here!* |
|||
</pre> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">str := " \t \r \n String with spaces \t \r \n "; |
||
with(StringTools): |
with(StringTools): |
||
Line 1,273: | Line 1,803: | ||
TrimLeft(str); |
TrimLeft(str); |
||
TrimRight(str); |
TrimRight(str); |
||
Trim(str);</ |
Trim(str);</syntaxhighlight> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">StringTrim[" \n\t string with spaces \n \t "]</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">% remove trailing whitespaces |
||
str = str(1:find(~isspace(str),1,'last')); |
str = str(1:find(~isspace(str),1,'last')); |
||
% remove leading whitespaces |
% remove leading whitespaces |
||
Line 1,289: | Line 1,819: | ||
% a built-in function, removes leading and trailing whitespaces |
% a built-in function, removes leading and trailing whitespaces |
||
str = strtrim(str);</ |
str = strtrim(str);</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
< |
<syntaxhighlight lang="mercury">:- module top_and_tail. |
||
:- interface. |
:- interface. |
||
Line 1,305: | Line 1,835: | ||
io.format("leading ws removed: %s\n", [s(lstrip(TestPhrase))], !IO), |
io.format("leading ws removed: %s\n", [s(lstrip(TestPhrase))], !IO), |
||
io.format("trailing ws removed: %s\n", [s(rstrip(TestPhrase))], !IO), |
io.format("trailing ws removed: %s\n", [s(rstrip(TestPhrase))], !IO), |
||
io.format("both removed: %s\b", [s(strip(TestPhrase))], !IO).</ |
io.format("both removed: %s\b", [s(strip(TestPhrase))], !IO).</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
< |
<syntaxhighlight lang="nemerle">def str = "\t\n\t A string with\nwhitespace\n\n\t "; |
||
WriteLine(str.TrimStart()); |
WriteLine(str.TrimStart()); |
||
WriteLine(str.TrimEnd()); |
WriteLine(str.TrimEnd()); |
||
WriteLine(str.Trim()); // both ends at once, of course, internal whitespace is preserved in all 3</ |
WriteLine(str.Trim()); // both ends at once, of course, internal whitespace is preserved in all 3</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
NetRexx provides a <tt>strip()</tt> method which can be used to remove all of a single character from the head and/or tail of a string. To remove ''all'' whitespace characters from a string requires a little more work: |
NetRexx provides a <tt>strip()</tt> method which can be used to remove all of a single character from the head and/or tail of a string. To remove ''all'' whitespace characters from a string requires a little more work: |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,404: | Line 1,934: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
<pre style="height: 25ex; overflow:scroll;"> |
<pre style="height: 25ex; overflow:scroll;"> |
||
Line 1,453: | Line 1,983: | ||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(setq str " this is a string ") |
||
;; trim leading blanks |
;; trim leading blanks |
||
Line 1,462: | Line 1,992: | ||
;; trim both leading and trailing blanks |
;; trim both leading and trailing blanks |
||
(trim str)</ |
(trim str)</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
let s = " |
let s = "\t \v \r String with spaces \n \t \f" |
||
echo " |
echo "“", s, "”" |
||
echo("*** Stripped of leading spaces ***") |
|||
echo "'", s.strip(trailing = false), "'" |
|||
echo " |
echo "“", s.strip(trailing = false), "”" |
||
echo("*** Stripped of trailing spaces ***") |
|||
echo "'", s.strip(), "'"</lang> |
|||
echo "“", s.strip(leading = false), "”" |
|||
echo("*** Stripped of leading and trailing spaces ***") |
|||
echo "“", s.strip(), "”" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre>“ |
|||
String with spaces |
|||
” |
|||
*** Stripped of leading spaces *** |
|||
“String with spaces |
|||
” |
|||
*** Stripped of trailing spaces *** |
|||
“ |
|||
String with spaces” |
|||
*** Stripped of leading and trailing spaces *** |
|||
“String with spaces”</pre> |
|||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Oxford Oberon-2 |
Oxford Oberon-2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE Trim; |
MODULE Trim; |
||
IMPORT Out,Strings,SYSTEM; |
IMPORT Out,Strings,SYSTEM; |
||
Line 1,534: | Line 2,083: | ||
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln; |
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln; |
||
END Trim. |
END Trim. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,546: | Line 2,095: | ||
[]= [] |
[]= [] |
||
</pre> |
</pre> |
||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
{{works with|Cocoa}} |
{{works with|Cocoa}} |
||
{{works with|GNUstep}} |
{{works with|GNUstep}} |
||
< |
<syntaxhighlight lang="objc">#import <Foundation/Foundation.h> |
||
@interface NSString (RCExt) |
@interface NSString (RCExt) |
||
Line 1,601: | Line 2,151: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let left_pos s len = |
||
let rec aux i = |
let rec aux i = |
||
if i >= len then None |
if i >= len then None |
||
Line 1,639: | Line 2,189: | ||
match right_pos s len with |
match right_pos s len with |
||
| Some i -> String.sub s 0 (i + 1) |
| Some i -> String.sub s 0 (i + 1) |
||
| None -> ""</ |
| None -> ""</syntaxhighlight> |
||
we put the previous code in a file called "trim.ml", and then we test these functions in the toplevel: |
we put the previous code in a file called "trim.ml", and then we test these functions in the toplevel: |
||
<pre>$ ocaml |
<pre>$ ocaml |
||
Line 1,656: | Line 2,206: | ||
# rtrim s ;; |
# rtrim s ;; |
||
- : string = " \t \r \n String with spaces"</pre> |
- : string = " \t \r \n String with spaces"</pre> |
||
Since OCaml version 4.00.0 there is a function '''String.trim'''. |
|||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang="progress">DEF VAR cc AS CHAR INIT " string with spaces ". |
||
MESSAGE |
MESSAGE |
||
Line 1,664: | Line 2,216: | ||
"|" + RIGHT-TRIM( cc ) + "|" SKIP |
"|" + RIGHT-TRIM( cc ) + "|" SKIP |
||
"|" + TRIM( cc ) + "|" |
"|" + TRIM( cc ) + "|" |
||
VIEW-AS ALERT-BOX.</ |
VIEW-AS ALERT-BOX.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,679: | Line 2,231: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub ltrim { shift =~ s/^\s+//r } |
||
sub rtrim { shift =~ s/\s+$//r } |
sub rtrim { shift =~ s/\s+$//r } |
||
sub trim { ltrim rtrim shift } |
sub trim { ltrim rtrim shift } |
||
Line 1,688: | Line 2,240: | ||
print "'", trim($p), "'\n"; |
print "'", trim($p), "'\n"; |
||
print "'", ltrim($p), "'\n"; |
print "'", ltrim($p), "'\n"; |
||
print "'", rtrim($p), "'\n";</ |
print "'", rtrim($p), "'\n";</syntaxhighlight> |
||
=={{header|Perl 6}}== |
|||
<lang perl6>my $s = "\r\n \t\x2029 Good Stuff \x202F\n"; |
|||
say $s.trim; |
|||
say $s.trim.perl; |
|||
say $s.trim-leading.perl; |
|||
say $s.trim-trailing.perl;</lang> |
|||
{{Output}} |
|||
<pre> |
|||
Good Stuff |
|||
"Good Stuff" |
|||
"Good Stuff \n" |
|||
"\r\n \t
Good Stuff" |
|||
</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
<lang Phix>constant s = "\ttest\n" |
|||
<!--<syntaxhighlight lang="phix">(phixonline)--> |
|||
?s |
|||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
|||
?trim_head(s) |
|||
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"\t test \n"</span> |
|||
?trim_tail(s) |
|||
<span style="color: #0000FF;">?</span><span style="color: #000000;">s</span> |
|||
?trim(s)</lang> |
|||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">trim_head</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">trim_tail</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
|||
<!--</syntaxhighlight>--> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
"\ |
"\t test \n" |
||
" |
"test \n" |
||
"\ |
"\t test" |
||
"test" |
"test" |
||
</pre> |
</pre> |
||
Line 1,721: | Line 2,262: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
There is a built-in function that already does this. |
There is a built-in function that already does this. |
||
< |
<syntaxhighlight lang="php"><?php |
||
/** |
/** |
||
Line 1,731: | Line 2,272: | ||
echo '^'.ltrim($string).'$'.PHP_EOL; |
echo '^'.ltrim($string).'$'.PHP_EOL; |
||
echo '^'.rtrim($string).'$'.PHP_EOL; |
echo '^'.rtrim($string).'$'.PHP_EOL; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,738: | Line 2,279: | ||
^ this is a string$ |
^ this is a string$ |
||
</pre> |
</pre> |
||
=={{header|Picat}}== |
|||
Using the built-in functions <code>lstrip/1</code>, <code>rstrip/1</code>, <code>strip/1</code>, and <code>strip/2</code>. |
|||
<syntaxhighlight lang="picat">import util. |
|||
go => |
|||
S = " Jabberwocky ", |
|||
println("<" ++ S ++ ">"), |
|||
println("<" ++ lstrip(S) ++ ">"), |
|||
println("<" ++ rstrip(S) ++ ">"), |
|||
println("<" ++ strip(S) ++ ">"), |
|||
println("<" ++ strip("\t\r\bTwas brillig, and the slithy toves\r \t\v"," \b\v\t\r\v") ++ ">"), |
|||
nl.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>< Jabberwocky > |
|||
<Jabberwocky > |
|||
< Jabberwocky> |
|||
<Jabberwocky> |
|||
<Twas brillig, and the slithy toves></pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de trimLeft (Str) |
||
(pack (flip (trim (flip (chop Str))))) ) |
(pack (flip (trim (flip (chop Str))))) ) |
||
Line 1,747: | Line 2,309: | ||
(de trimBoth (Str) |
(de trimBoth (Str) |
||
(pack (clip (chop Str))) )</ |
(pack (clip (chop Str))) )</syntaxhighlight> |
||
Test: |
Test: |
||
<pre>: (trimLeft " ^G ^I trimmed left ^L ") |
<pre>: (trimLeft " ^G ^I trimmed left ^L ") |
||
Line 1,759: | Line 2,321: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">put ( trim(text, ' ', '') ); /* trims leading blanks. */ |
||
put ( trim(text, '', ' ') ); /* trims trailing blanks. */ |
put ( trim(text, '', ' ') ); /* trims trailing blanks. */ |
||
put ( trim(text) ); /* trims leading and trailing */ |
put ( trim(text) ); /* trims leading and trailing */ |
||
/* blanks. */</ |
/* blanks. */</syntaxhighlight> |
||
To remove any white-space character(s) in a portable way:- |
To remove any white-space character(s) in a portable way:- |
||
< |
<syntaxhighlight lang="pli">declare whitespace character(33) value |
||
((substr(collate(), 1, 32) || ' ')); |
((substr(collate(), 1, 32) || ' ')); |
||
put ( trim(text, whitespace) ); /* trims leading white space. */ |
put ( trim(text, whitespace) ); /* trims leading white space. */ |
||
Line 1,770: | Line 2,332: | ||
put ( trim(text, whitespace, whitespace) ); |
put ( trim(text, whitespace, whitespace) ); |
||
/* trims leading and trailing */ |
/* trims leading and trailing */ |
||
/* white space. */</ |
/* white space. */</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$var = " Hello World " |
$var = " Hello World " |
||
$var.TrimStart() # String with leading whitespace removed |
$var.TrimStart() # String with leading whitespace removed |
||
$var.TrimEnd() # String with trailing whitespace removed |
$var.TrimEnd() # String with trailing whitespace removed |
||
$var.Trim() # String with both leading and trailing whitespace removed |
$var.Trim() # String with both leading and trailing whitespace removed |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 1,787: | Line 2,350: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works with SWI-Prolog. |
Works with SWI-Prolog. |
||
< |
<syntaxhighlight lang="prolog">strip :- |
||
In = " There are unwanted blanks here! ", |
In = " There are unwanted blanks here! ", |
||
strip_left(In, OutLeft), |
strip_left(In, OutLeft), |
||
Line 1,816: | Line 2,379: | ||
strip_action(X) --> X. |
strip_action(X) --> X. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output : |
Output : |
||
<pre> ?- strip. |
<pre> ?- strip. |
||
Line 1,828: | Line 2,391: | ||
In = ' There are unwanted blanks here! ', |
In = ' There are unwanted blanks here! ', |
||
Out = 'There are unwanted blanks here!'. |
Out = 'There are unwanted blanks here!'. |
||
</pre> |
|||
=== alternative using DCG strings === |
|||
{{works with|SWI-Prolog|7}} |
|||
<syntaxhighlight lang="prolog"> |
|||
:- system:set_prolog_flag(double_quotes,chars) . |
|||
strip_leading(INPUTz,OUTPUTz) |
|||
:- |
|||
prolog:phrase(strip_leading(OUTPUTz),INPUTz) |
|||
. |
|||
strip_trailing(INPUTz,OUTPUTz) |
|||
:- |
|||
prolog:phrase(strip_trailing(OUTPUTz),INPUTz) |
|||
. |
|||
strip_leading_and_trailing(INPUTz,OUTPUTz) |
|||
:- |
|||
prolog:phrase(strip_leading_and_trailing(OUTPUTz),INPUTz) |
|||
. |
|||
strip_leading(OUTPUTz) |
|||
--> |
|||
strip_leading_spacious , |
|||
strip_leading_nonspacious(OUTPUTz) |
|||
. |
|||
strip_trailing(OUTPUTz) |
|||
--> |
|||
strip_trailing_nonspacious(OUTPUTz) , |
|||
strip_trailing_spacious |
|||
. |
|||
strip_leading_and_trailing(OUTPUTz) |
|||
--> |
|||
strip_leading_spacious , |
|||
strip_trailing_nonspacious(OUTPUTz) , |
|||
strip_trailing_spacious |
|||
. |
|||
strip_leading_spacious |
|||
--> |
|||
[CHAR] , |
|||
{ prolog:char_type(CHAR,space) } , |
|||
! , |
|||
strip_leading_spacious |
|||
. |
|||
strip_leading_spacious |
|||
--> |
|||
[] |
|||
. |
|||
strip_leading_nonspacious([OUTPUT|OUTPUTz]) |
|||
--> |
|||
[OUTPUT] , |
|||
! , |
|||
strip_leading_nonspacious(OUTPUTz) |
|||
. |
|||
strip_leading_nonspacious([]) |
|||
--> |
|||
[] |
|||
. |
|||
strip_trailing_nonspacious([OUTPUT|OUTPUTz]) |
|||
--> |
|||
\+ strip_trailing_spacious , |
|||
[OUTPUT] , |
|||
! , |
|||
strip_trailing_nonspacious(OUTPUTz) |
|||
. |
|||
strip_trailing_nonspacious([]) |
|||
--> |
|||
[] |
|||
. |
|||
strip_trailing_spacious |
|||
--> |
|||
[CHAR] , |
|||
{ prolog:char_type(CHAR,space) } , |
|||
! , |
|||
strip_trailing_spacious |
|||
. |
|||
strip_trailing_spacious |
|||
--> |
|||
\+ [_] % .i.e. at end-of-string . |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
/* |
|||
?- strip_leading(" Hello World ",OUTPUTz) . |
|||
OUTPUTz = ['H', e, l, l, o, ' ', 'W', o, r, l, d, ' ']. |
|||
?- strip_trailing(" Hello World ",OUTPUTz). |
|||
OUTPUTz = [' ', 'H', e, l, l, o, ' ', 'W', o, r, l, d]. |
|||
?- strip_leading_and_trailing(" Hello World ",OUTPUTz). |
|||
OUTPUTz = ['H', e, l, l, o, ' ', 'W', o, r, l, d]. |
|||
?- |
|||
</pre> |
</pre> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Note, if only spaces need to be removed, PureBasic provides commands that do this: <tt>LTrim()</tt>, <tt>RTrim()</tt>, and <tt>Trim()</tt>. To handle a larger selection of whitespace the following functions meet the task. |
Note, if only spaces need to be removed, PureBasic provides commands that do this: <tt>LTrim()</tt>, <tt>RTrim()</tt>, and <tt>Trim()</tt>. To handle a larger selection of whitespace the following functions meet the task. |
||
< |
<syntaxhighlight lang="purebasic">;define the whitespace as desired |
||
#whitespace$ = " " + Chr($9) + Chr($A) + Chr($B) + Chr($C) + Chr($D) + Chr($1C) + Chr($1D) + Chr($1E) + Chr($1F) |
#whitespace$ = " " + Chr($9) + Chr($A) + Chr($B) + Chr($C) + Chr($D) + Chr($1C) + Chr($1D) + Chr($1E) + Chr($1F) |
||
Line 1,868: | Line 2,540: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"Top " |
<pre>"Top " |
||
Line 1,875: | Line 2,547: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">>>> s = ' \t \r \n String with spaces \t \r \n ' |
||
>>> s |
>>> s |
||
' \t \r \n String with spaces \t \r \n ' |
' \t \r \n String with spaces \t \r \n ' |
||
Line 1,884: | Line 2,556: | ||
>>> s.strip() |
>>> s.strip() |
||
'String with spaces' |
'String with spaces' |
||
>>> </ |
>>> </syntaxhighlight> |
||
=={{header|Quackery}}== |
|||
As a dialogue in the Quackery shell. |
|||
<syntaxhighlight lang="quackery">/O> [ reverse trim reverse ] is trim-trailing ( $ --> $ ) |
|||
... [ trim trim-trailing ] is trim-both-ends ( $ --> $ ) |
|||
... $ ' a quick quack ' echo$ say "!" cr |
|||
... $ ' a quick quack ' trim echo$ say "!" cr |
|||
... $ ' a quick quack ' trim-trailing echo$ say "!" cr |
|||
... $ ' a quick quack ' trim-both-ends echo$ say "!" cr |
|||
... |
|||
a quick quack ! |
|||
a quick quack ! |
|||
a quick quack! |
|||
a quick quack! |
|||
Stack empty.</syntaxhighlight> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="r">s <- " Ars Longa " |
|||
trimws(s) |
|||
[1] "Ars Longa" |
|||
trimws(s, "left") |
|||
[1] "Ars Longa " |
|||
trimws(s, "right") |
|||
[1] " Ars Longa"</syntaxhighlight> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
Line 1,904: | Line 2,606: | ||
;; can also normalize spaces: |
;; can also normalize spaces: |
||
(string-normalize-spaces (string-trim str)) ; -> "foo bar" |
(string-normalize-spaces (string-trim str)) ; -> "foo bar" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header| |
=={{header|Raku}}== |
||
(formerly Perl 6) |
|||
<lang Retro>" this is a test " ^strings'trimLeft |
|||
{{works with|Rakudo|2020.08.1}} |
|||
" this is a test " ^strings'trimRight |
|||
<syntaxhighlight lang="raku" line>my $s = "\r\n \t\x2029 Good Stuff \x202F\n"; |
|||
" this is a test " ^strings'trimLeft ^strings'trimRight</lang> |
|||
say $s.trim; |
|||
say $s.trim.raku; |
|||
say $s.trim-leading.raku; |
|||
say $s.trim-trailing.raku;</syntaxhighlight> |
|||
{{Output}} |
|||
<pre> |
|||
Good Stuff |
|||
"Good Stuff" |
|||
"Good Stuff \n" |
|||
"\r\n \t
Good Stuff" |
|||
</pre> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="red">>> trim/head " remove leading white space " |
||
== "remove leading white space " |
== "remove leading white space " |
||
>> trim/tail " remove trailing white space " |
>> trim/tail " remove trailing white space " |
||
== " remove trailing white space" |
== " remove trailing white space" |
||
>> trim " remove both white spaces " |
>> trim " remove both white spaces " |
||
== "remove both white spaces"</ |
== "remove both white spaces"</syntaxhighlight> |
||
=={{header|Retro}}== |
|||
<syntaxhighlight lang="retro">'__this_is_a_test___ s:trim-left |
|||
'__this_is_a_test___ s:trim-right |
|||
'__this_is_a_test___ s:trim</syntaxhighlight> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program demonstrates how to strip leading and/or trailing spaces (blanks). */ |
||
yyy=" this is a string that has leading/embedded/trailing blanks, fur shure. " |
yyy=" this is a string that has leading/embedded/trailing blanks, fur shure. " |
||
say 'YYY──►'yyy"◄──" /*display the original string + fence. */ |
say 'YYY──►'yyy"◄──" /*display the original string + fence. */ |
||
Line 1,949: | Line 2,668: | ||
noX=space(yyy) /* including white space between words.*/ |
noX=space(yyy) /* including white space between words.*/ |
||
say 'nox──►'noX"◄──" /*display the string with a title+fence*/ |
say 'nox──►'noX"◄──" /*display the string with a title+fence*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 1,960: | Line 2,679: | ||
===version 2=== |
===version 2=== |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* 01.1.2012 Walter Pachl taking care of all non-printable chars |
* 01.1.2012 Walter Pachl taking care of all non-printable chars |
||
**********************************************************************/ |
**********************************************************************/ |
||
Line 1,984: | Line 2,703: | ||
Say 'sa>'sa'<'<' /* between words */ |
Say 'sa>'sa'<'<' /* between words */ |
||
s0=space(sa,0) /* remove all whitespace */ |
s0=space(sa,0) /* remove all whitespace */ |
||
Say 's0>'s0'<'</ |
Say 's0>'s0'<'</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,997: | Line 2,716: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
aList = " Welcome to the Ring Programming Language " |
aList = " Welcome to the Ring Programming Language " |
||
see aList + nl |
see aList + nl |
||
see trim(aList) + nl |
see trim(aList) + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|RPL}}== |
|||
Recursivity provides short code. |
|||
≪ |
|||
'''IF''' DUP 1 1 SUB " " == '''THEN''' |
|||
2 OVER SIZE SUB '''TRIM END''' |
|||
'''IF''' DUP DUP SIZE DUP SUB " " == '''THEN''' |
|||
1 OVER SIZE 1 - SUB '''TRIM END''' |
|||
≫ ‘'''TRIM'''’ STO |
|||
" What if true luxury was space? " '''TRIM''' |
|||
{{out}} |
|||
<pre> |
|||
1: "What if true luxury was space?" |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">s = " \t\v\r\n\ffoo bar \t\v\r\n\f" |
||
p s |
p s |
||
p s.lstrip # remove leading whitespaces |
p s.lstrip # remove leading whitespaces |
||
p s.rstrip # remove trailing whitespaces |
p s.rstrip # remove trailing whitespaces |
||
p s.strip # remove both leading and trailing whitespace |
p s.strip # remove both leading and trailing whitespace |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,019: | Line 2,753: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">string$ = " abcdefg " |
||
print " Top:";trim$(string$+"|") ' top left trim |
print " Top:";trim$(string$+"|") ' top left trim |
||
print "Bottom:";trim$("|"+string$) ' bottom right trim |
print "Bottom:";trim$("|"+string$) ' bottom right trim |
||
print " Both:";trim$(string$) ' both left and right |
print " Both:";trim$(string$) ' both left and right |
||
end</ |
end</syntaxhighlight> |
||
<pre> Top:abcdefg |
<pre> Top:abcdefg |
||
Bottom: abcdefg |
Bottom: abcdefg |
||
Line 2,030: | Line 2,764: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
let spaces = " \t\n\x0B\x0C\r \u{A0} \u{2000}\u{3000}"; |
let spaces = " \t\n\x0B\x0C\r \u{A0} \u{2000}\u{3000}"; |
||
let string_with_spaces = spaces.to_owned() + "String without spaces" + spaces; |
let string_with_spaces = spaces.to_owned() + "String without spaces" + spaces; |
||
Line 2,037: | Line 2,771: | ||
assert_eq!(string_with_spaces.trim_left(), "String without spaces".to_owned() + spaces); |
assert_eq!(string_with_spaces.trim_left(), "String without spaces".to_owned() + spaces); |
||
assert_eq!(string_with_spaces.trim_right(), spaces.to_owned() + "String without spaces"); |
assert_eq!(string_with_spaces.trim_right(), spaces.to_owned() + "String without spaces"); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang="sather">class MAIN is |
||
ltrim(s :STR) :STR is |
ltrim(s :STR) :STR is |
||
i ::= 0; |
i ::= 0; |
||
Line 2,076: | Line 2,810: | ||
#OUT + trim(p).pretty + "\n"; |
#OUT + trim(p).pretty + "\n"; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def trimLeft(str: String) = str dropWhile(_.isWhitespace) |
||
def trimRight(str: String) = str take (str.lastIndexWhere(!_.isWhitespace) + 1) |
def trimRight(str: String) = str take (str.lastIndexWhere(!_.isWhitespace) + 1) |
||
Line 2,094: | Line 2,828: | ||
println("trimRight2: |" + trimRight2(str) + "|") |
println("trimRight2: |" + trimRight2(str) + "|") |
||
println("trim : |" + trim(str) + "|") |
println("trim : |" + trim(str) + "|") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>original : | � String with spaces |
<pre>original : | � String with spaces |
||
Line 2,107: | Line 2,841: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 2,116: | Line 2,850: | ||
writeln(rtrim(testStri)); |
writeln(rtrim(testStri)); |
||
writeln(trim(testStri)); |
writeln(trim(testStri)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var s = " \t\v\r\n\ffoo bar \t\v\r\n\f"; |
||
say s.strip_beg.dump; # remove leading whitespaces |
say s.strip_beg.dump; # remove leading whitespaces |
||
say s.strip_end.dump; # remove trailing whitespaces |
say s.strip_end.dump; # remove trailing whitespaces |
||
say s.strip.dump; # remove both leading and trailing whitespace</ |
say s.strip.dump; # remove both leading and trailing whitespace</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,132: | Line 2,866: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|GNU Smalltalk}} |
{{works with|GNU Smalltalk}} |
||
< |
<syntaxhighlight lang="smalltalk">String extend |
||
[ |
[ |
||
ltrim [ |
ltrim [ |
||
Line 2,151: | Line 2,885: | ||
('"%1"' % {a ltrim}) displayNl. |
('"%1"' % {a ltrim}) displayNl. |
||
('"%1"' % {a rtrim}) displayNl. |
('"%1"' % {a rtrim}) displayNl. |
||
('"%1"' % {a trim}) displayNl.</ |
('"%1"' % {a trim}) displayNl.</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol4"> s1 = s2 = " Hello, people of earth! " |
||
s2 = CHAR(3) s2 CHAR(134) |
s2 = CHAR(3) s2 CHAR(134) |
||
&ALPHABET TAB(33) . prechars |
&ALPHABET TAB(33) . prechars |
||
Line 2,172: | Line 2,906: | ||
s2 POS(0) SPAN(stripchars) = |
s2 POS(0) SPAN(stripchars) = |
||
OUTPUT = "Full trim: >" s2 "<" |
OUTPUT = "Full trim: >" s2 "<" |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,181: | Line 2,915: | ||
Trailing: >� Hello, people of earth!< |
Trailing: >� Hello, people of earth!< |
||
Full trim: >Hello, people of earth!<</pre> |
Full trim: >Hello, people of earth!<</pre> |
||
=={{header|Standard ML}}== |
|||
<syntaxhighlight lang="sml">local |
|||
open Substring |
|||
in |
|||
val trimLeft = string o dropl Char.isSpace o full |
|||
and trimRight = string o dropr Char.isSpace o full |
|||
and trim = string o dropl Char.isSpace o dropr Char.isSpace o full |
|||
end</syntaxhighlight> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
See '''[https://www.stata.com/help.cgi?mf_strtrim strtrim]''' in Stata help. Use the equivalent '''[https://www.stata.com/help.cgi?mf_ustrtrim ustrtrim]''' functions with Unicode strings. |
See '''[https://www.stata.com/help.cgi?mf_strtrim strtrim]''' in Stata help. Use the equivalent '''[https://www.stata.com/help.cgi?mf_ustrtrim ustrtrim]''' functions with Unicode strings. |
||
< |
<syntaxhighlight lang="stata">s = " ars longa " |
||
"("+strtrim(s)+")" |
"("+strtrim(s)+")" |
||
(ars longa) |
(ars longa) |
||
Line 2,196: | Line 2,939: | ||
"("+stritrim(s)+")" |
"("+stritrim(s)+")" |
||
( ars longa )</ |
( ars longa )</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Whitespace stripping is done with <code>string trim</code> and related commands: |
Whitespace stripping is done with <code>string trim</code> and related commands: |
||
< |
<syntaxhighlight lang="tcl">set str " hello world " |
||
puts "original: >$str<" |
puts "original: >$str<" |
||
puts "trimmed head: >[string trimleft $str]<" |
puts "trimmed head: >[string trimleft $str]<" |
||
puts "trimmed tail: >[string trimright $str]<" |
puts "trimmed tail: >[string trimright $str]<" |
||
puts "trimmed both: >[string trim $str]<"</ |
puts "trimmed both: >[string trim $str]<"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,214: | Line 2,957: | ||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
< |
<syntaxhighlight lang="ti-83b"> |
||
PROGRAM:WHITESPC |
PROGRAM:WHITESPC |
||
Input Str1 |
Input Str1 |
||
Line 2,242: | Line 2,985: | ||
Lbl F |
Lbl F |
||
Disp "'"+Str1+"'" |
Disp "'"+Str1+"'" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
Line 2,263: | Line 3,006: | ||
" yep" |
" yep" |
||
"yep" |
"yep" |
||
=={{header|True BASIC}}== |
|||
<syntaxhighlight lang="qbasic">LET s$ = " \tRosetta Code \v\f\r\n" |
|||
PRINT ltrim$(s$) ! remove leading whitespace |
|||
PRINT rtrim$(s$) ! remove trailing whitespace |
|||
PRINT trim$(s$) ! remove both leading and trailing whitespace |
|||
END</syntaxhighlight> |
|||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript">$$ MODE TUSCRIPT |
||
str= " sentence w/whitespace before and after " |
str= " sentence w/whitespace before and after " |
||
trimmedtop=EXTRACT (str,":<|<> :"|,0) |
trimmedtop=EXTRACT (str,":<|<> :"|,0) |
||
Line 2,273: | Line 3,024: | ||
PRINT "trimmed on top <|",trimmedtop,">|" |
PRINT "trimmed on top <|",trimmedtop,">|" |
||
PRINT "trimmed on tail <|", trimmedtail,">|" |
PRINT "trimmed on tail <|", trimmedtail,">|" |
||
PRINT "trimmed on both <|", trimmedboth,">|"</ |
PRINT "trimmed on both <|", trimmedboth,">|"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,286: | Line 3,037: | ||
===Pattern Matching Language Exercise=== |
===Pattern Matching Language Exercise=== |
||
Here, no builtin functions are used, just text pattern matching logic. Two functions are written, conforming to the proper filter convention, and then employed as filters. |
Here, no builtin functions are used, just text pattern matching logic. Two functions are written, conforming to the proper filter convention, and then employed as filters. |
||
< |
<syntaxhighlight lang="txr">@(define trim_left (in out)) |
||
@ (next :list in) |
@ (next :list in) |
||
@/[ \t]*/@out |
@/[ \t]*/@out |
||
Line 2,305: | Line 3,056: | ||
trim_right: [@{line_of_input :filter (:fun trim_right)}] |
trim_right: [@{line_of_input :filter (:fun trim_right)}] |
||
trim_both: [@{line_of_input :filter ((:fun trim_left) (:fun trim_right))}] |
trim_both: [@{line_of_input :filter ((:fun trim_left) (:fun trim_right))}] |
||
@(end)</ |
@(end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>$ echo "" | txr trim.txr - |
<pre>$ echo "" | txr trim.txr - |
||
Line 2,332: | Line 3,083: | ||
Trimming whitespace from both ends is a builtin: |
Trimming whitespace from both ends is a builtin: |
||
< |
<syntaxhighlight lang="sh">$ txr -p '(trim-str " a b ")' |
||
"a b"</ |
"a b"</syntaxhighlight> |
||
An unnecessarily cryptic, though educational, left trim: |
An unnecessarily cryptic, though educational, left trim: |
||
< |
<syntaxhighlight lang="sh">$ txr -p '[(do progn (del [@1 0..(match-regex @1 #/\s*/)]) @1) " a b "]' |
||
"a b "</ |
"a b "</syntaxhighlight> |
||
Explanation: the basic structure is <code>[function " a b "]</code> where the function is an anonymous lambda generated using the <code>do</code> operator. The function is applied to the string <code>" a b "</code>. |
Explanation: the basic structure is <code>[function " a b "]</code> where the function is an anonymous lambda generated using the <code>do</code> operator. The function is applied to the string <code>" a b "</code>. |
||
Line 2,348: | Line 3,099: | ||
Lastly, a pedestrian right trim: |
Lastly, a pedestrian right trim: |
||
< |
<syntaxhighlight lang="txrlisp">(defun trim-right (str) |
||
(for () |
(for () |
||
((and (> (length str) 0) (chr-isspace [str -1])) str) |
((and (> (length str) 0) (chr-isspace [str -1])) str) |
||
Line 2,355: | Line 3,106: | ||
(format t "{~a}\n" (trim-right " ")) |
(format t "{~a}\n" (trim-right " ")) |
||
(format t "{~a}\n" (trim-right "a ")) |
(format t "{~a}\n" (trim-right "a ")) |
||
(format t "{~a}\n" (trim-right ""))</ |
(format t "{~a}\n" (trim-right ""))</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,363: | Line 3,114: | ||
{a} |
{a} |
||
{}</pre> |
{}</pre> |
||
=={{header|Vala}}== |
|||
===Strip Leading White Space=== |
|||
<lang vala>string s = " word "; |
|||
string s_chug = s.chug();</lang> |
|||
===Strip Trailing White Space=== |
|||
<lang vala>string s = " word "; |
|||
string s_chomp = s.chomp();</lang> |
|||
===Strip Leading & Trailing White Space=== |
|||
<lang vala>string s = " word "; |
|||
string s_strip = s.strip();</lang> |
|||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
white = ==` !| not @iNC %sI |
white = ==` !| not @iNC %sI |
||
Line 2,385: | Line 3,125: | ||
#cast %sgUL |
#cast %sgUL |
||
main = <.trim_left,trim_right,trim_both> ' string with spaces '</ |
main = <.trim_left,trim_right,trim_both> ' string with spaces '</syntaxhighlight> |
||
* The <code>white</code> predicate tests an argument for whiteness by either comparing it to a literal space character or testing whether the singleton list containing it is of a string (<code>%s</code>) type. |
* The <code>white</code> predicate tests an argument for whiteness by either comparing it to a literal space character or testing whether the singleton list containing it is of a string (<code>%s</code>) type. |
||
* The <code>-~</code> postfix operator takes a predicate to a function that takes a string to a pair of strings whose concatenation is the original string and whose left side is the maximal prefix of the original string whose members satisfy the predicate. |
* The <code>-~</code> postfix operator takes a predicate to a function that takes a string to a pair of strings whose concatenation is the original string and whose left side is the maximal prefix of the original string whose members satisfy the predicate. |
||
Line 2,396: | Line 3,136: | ||
' string with spaces', |
' string with spaces', |
||
'string with spaces'></pre> |
'string with spaces'></pre> |
||
=={{header|Vala}}== |
|||
===Strip Leading White Space=== |
|||
<syntaxhighlight lang="vala">string s = " word "; |
|||
string s_chug = s.chug();</syntaxhighlight> |
|||
===Strip Trailing White Space=== |
|||
<syntaxhighlight lang="vala">string s = " word "; |
|||
string s_chomp = s.chomp();</syntaxhighlight> |
|||
===Strip Leading & Trailing White Space=== |
|||
<syntaxhighlight lang="vala">string s = " word "; |
|||
string s_strip = s.strip();</syntaxhighlight> |
|||
=={{header|VBA}}== |
|||
<syntaxhighlight lang="vb">Public Sub test() |
|||
'LTrim trims leading spaces |
|||
'RTrim trims tailing spaces |
|||
'Trim trims both leading and tailing spaces |
|||
s = " trim " |
|||
Debug.Print """" & s & """" |
|||
Debug.Print """" & LTrim(s) & """" |
|||
Debug.Print """" & RTrim(s) & """" |
|||
Debug.Print """" & WorksheetFunction.trim(s) & """" |
|||
'these functions do not remove tabs or newlines |
|||
End Sub</syntaxhighlight>{{out}} |
|||
<pre>" trim " |
|||
"trim " |
|||
" trim" |
|||
"trim" |
|||
</pre> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function LeftTrim(s) |
Function LeftTrim(s) |
||
Set regex = New RegExp |
Set regex = New RegExp |
||
Line 2,427: | Line 3,196: | ||
WScript.StdOut.WriteLine RightTrim("RosettaCode ") |
WScript.StdOut.WriteLine RightTrim("RosettaCode ") |
||
WScript.StdOut.WriteLine LeftTrim(RightTrim(" RosettaCode ")) |
WScript.StdOut.WriteLine LeftTrim(RightTrim(" RosettaCode ")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,435: | Line 3,204: | ||
RosettaCode |
RosettaCode |
||
</pre> |
</pre> |
||
=={{header|Wren}}== |
|||
In Wren 'whitespace' is defined as: space, tab, carriage return, and line feed characters. To trim off other non-printing characters such as form feed and vertical tab, you need to do a little more work. |
|||
<syntaxhighlight lang="wren">var a = " \t\r\nString with leading whitespace removed" |
|||
var b = "String with trailing whitespace removed \t\r\n" |
|||
var c = " \t\r\nString with both leading and trailing whitespace removed \t\r\n" |
|||
var d = " \t\r\n\f\vString with leading whitespace, form feed and verical tab characters removed" |
|||
System.print("'%(a.trimStart())'") |
|||
System.print("'%(b.trimEnd())'") |
|||
System.print("'%(c.trim())'") |
|||
System.print("'%(d.trimStart(" \t\r\n\f\v"))'") // similar overloads of trimEnd and trim exist</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
'String with leading whitespace removed' |
|||
'String with trailing whitespace removed' |
|||
'String with both leading and trailing whitespace removed' |
|||
'String with leading whitespace, form feed and vertical tab characters removed' |
|||
</pre> |
|||
=={{header|XBasic}}== |
|||
{{works with|Windows XBasic}} |
|||
<syntaxhighlight lang="xbasic">PROGRAM "progname" |
|||
VERSION "0.0000" |
|||
DECLARE FUNCTION Entry () |
|||
FUNCTION Entry () |
|||
s$ = " \tRosetta Code \v\f\r\n" |
|||
PRINT LTRIM$(s$) ' remove leading whitespace |
|||
PRINT RTRIM$(s$) ' remove trailing whitespace |
|||
PRINT TRIM$(s$) ' remove both leading and trailing whitespace |
|||
END FUNCTION |
|||
END PROGRAM</syntaxhighlight> |
|||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code ChOut=8, CrLf=9, Text=12; |
||
string 0; \use zero-terminated string convention |
string 0; \use zero-terminated string convention |
||
Line 2,473: | Line 3,278: | ||
Text(0, StripTrail(String)); ChOut(0, ^!); CrLf(0); |
Text(0, StripTrail(String)); ChOut(0, ^!); CrLf(0); |
||
Text(0, StripTrail(StripLead(String))); ChOut(0, ^!); CrLf(0); |
Text(0, StripTrail(StripLead(String))); ChOut(0, ^!); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,481: | Line 3,286: | ||
Hello World! |
Hello World! |
||
</pre> |
</pre> |
||
=={{header|Yabasic}}== |
|||
<syntaxhighlight lang="yabasic">s$ = "\t test \n" |
|||
print "--",ltrim$(s$),"--" |
|||
print "--",rtrim$(s$),"--" |
|||
print "--",trim$(s$),"--"</syntaxhighlight> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Remove white space from both end of string: |
Remove white space from both end of string: |
||
< |
<syntaxhighlight lang="zkl">"\t\n hoho\n\t\ ".strip() //-->"hoho"</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn removeLeadingWS(s){ n:=0; |
||
try{ while(s[n].isSpace()){ n+=1 } }catch{""}fallthrough{ s[n,*] } |
try{ while(s[n].isSpace()){ n+=1 } }catch{""}fallthrough{ s[n,*] } |
||
} |
} |
||
removeLeadingWS("\t\n hoho\n\t\ ") //-->"hoho\n\t " |
removeLeadingWS("\t\n hoho\n\t\ ") //-->"hoho\n\t " |
||
removeLeadingWS("") //-->""</ |
removeLeadingWS("") //-->""</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn removeTrailingWS(s){ n:=-1; |
||
try{ while(s[n].isSpace()){ n-=1 } s[0,n+1] }catch{""} |
try{ while(s[n].isSpace()){ n-=1 } s[0,n+1] }catch{""} |
||
} |
} |
||
removeTrailingWS("\t\n hoho\n\t\ ") //-->"\t\n hoho" |
removeTrailingWS("\t\n hoho\n\t\ ") //-->"\t\n hoho" |
||
removeTrailingWS("\t\n \n\t\ ") //-->""</ |
removeTrailingWS("\t\n \n\t\ ") //-->""</syntaxhighlight> |
||
=={{header|Zoea}}== |
|||
<syntaxhighlight lang="zoea"> |
|||
program: trim_left |
|||
input: ' abcd' |
|||
output: 'abcd' |
|||
program: trim_right |
|||
input: 'abcd ' |
|||
output: 'abcd' |
|||
program: trim |
|||
input: ' abcd ' |
|||
output: 'abcd' |
|||
</syntaxhighlight> |
|||
=={{header|Zoea Visual}}== |
|||
[http://zoea.co.uk/examples/zv-rc/Trim.png Strip whitespace from a string/Top and tail] |
Revision as of 16:28, 10 February 2024
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Demonstrate how to strip leading and trailing whitespace from a string.
The solution should demonstrate how to achieve the following three results:
- String with leading whitespace removed
- String with trailing whitespace removed
- String with both leading and trailing whitespace removed
For the purposes of this task whitespace includes non printable characters such as the space character, the tab character, and other such characters that have no corresponding graphical representation.
- Metrics
- Counting
- Word frequency
- Letter frequency
- Jewels and stones
- I before E except after C
- Bioinformatics/base count
- Count occurrences of a substring
- Count how many vowels and consonants occur in a string
- Remove/replace
- XXXX redacted
- Conjugate a Latin verb
- Remove vowels from a string
- String interpolation (included)
- Strip block comments
- Strip comments from a string
- Strip a set of characters from a string
- Strip whitespace from a string -- top and tail
- Strip control codes and extended characters from a string
- Anagrams/Derangements/shuffling
- Word wheel
- ABC problem
- Sattolo cycle
- Knuth shuffle
- Ordered words
- Superpermutation minimisation
- Textonyms (using a phone text pad)
- Anagrams
- Anagrams/Deranged anagrams
- Permutations/Derangements
- Find/Search/Determine
- ABC words
- Odd words
- Word ladder
- Semordnilap
- Word search
- Wordiff (game)
- String matching
- Tea cup rim text
- Alternade words
- Changeable words
- State name puzzle
- String comparison
- Unique characters
- Unique characters in each string
- Extract file extension
- Levenshtein distance
- Palindrome detection
- Common list elements
- Longest common suffix
- Longest common prefix
- Compare a list of strings
- Longest common substring
- Find common directory path
- Words from neighbour ones
- Change e letters to i in words
- Non-continuous subsequences
- Longest common subsequence
- Longest palindromic substrings
- Longest increasing subsequence
- Words containing "the" substring
- Sum of the digits of n is substring of n
- Determine if a string is numeric
- Determine if a string is collapsible
- Determine if a string is squeezable
- Determine if a string has all unique characters
- Determine if a string has all the same characters
- Longest substrings without repeating characters
- Find words which contains all the vowels
- Find words which contains most consonants
- Find words which contains more than 3 vowels
- Find words which first and last three letters are equals
- Find words which odd letters are consonants and even letters are vowels or vice_versa
- Formatting
- Substring
- Rep-string
- Word wrap
- String case
- Align columns
- Literals/String
- Repeat a string
- Brace expansion
- Brace expansion using ranges
- Reverse a string
- Phrase reversals
- Comma quibbling
- Special characters
- String concatenation
- Substring/Top and tail
- Commatizing numbers
- Reverse words in a string
- Suffixation of decimal numbers
- Long literals, with continuations
- Numerical and alphabetical suffixes
- Abbreviations, easy
- Abbreviations, simple
- Abbreviations, automatic
- Song lyrics/poems/Mad Libs/phrases
- Mad Libs
- Magic 8-ball
- 99 Bottles of Beer
- The Name Game (a song)
- The Old lady swallowed a fly
- The Twelve Days of Christmas
- Tokenize
- Text between
- Tokenize a string
- Word break problem
- Tokenize a string with escaping
- Split a character string based on change of character
- Sequences
11l
V s = " \t \r \n String with spaces \t \r \n "
print(s.ltrim((‘ ’, "\t", "\r", "\n")).len)
print(s.rtrim((‘ ’, "\t", "\r", "\n")).len)
print(s.trim((‘ ’, "\t", "\r", "\n")).len)
- Output:
29 25 18
Action!
DEFINE SPACE="32"
DEFINE TAB="127"
DEFINE NEWLINE="155"
BYTE FUNC IsWhitespace(CHAR c)
IF c=SPACE OR c=TAB OR c=NEWLINE THEN
RETURN (1)
FI
RETURN (0)
PROC Strip(CHAR ARRAY src,dst BYTE head,tail)
BYTE i,first,last
first=1
last=src(0)
IF head THEN
WHILE first<=last
DO
IF IsWhitespace(src(first))=0 THEN
EXIT
FI
first==+1
OD
FI
IF tail THEN
WHILE last>=first
DO
IF IsWhitespace(src(last))=0 THEN
EXIT
FI
last==-1
OD
FI
IF first>last THEN
dst(0)=0
ELSE
SCopyS(dst,src,first,last)
FI
RETURN
PROC Main()
CHAR ARRAY src=" Action! ",dst(20)
src(2)=NEWLINE src(13)=TAB
PrintF("Original string:%E""%S""%E%E",src)
Strip(src,dst,1,0)
PrintF("Trim leading whitespaces:%E""%S""%E%E",dst)
Strip(src,dst,0,1)
PrintF("Trim trailing whitespaces:%E""%S""%E%E",dst)
Strip(src,dst,1,1)
PrintF("Trim leading and trailing whitespaces:""%S""%E%E",dst)
RETURN
- Output:
Screenshot from Atari 8-bit computer
Original string: " Action! " Trim leading whitespaces: "Action! " Trim trailing whitespaces: " Action!" Trim leading and trailing whitespaces: "Action!"
Ada
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
str : String := " Jabberwocky ";
begin
Put_Line ("'" & Trim (str, Left) & "'");
Put_Line ("'" & Trim (str, Right) & "'");
Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
- Output:
'Jabberwocky ' ' Jabberwocky' 'Jabberwocky'
ALGOL 68
version 1
# returns "text" with leading non-printing characters removed #
PROC trim leading whitespace = ( STRING text )STRING:
BEGIN
INT pos := LWB text;
WHILE
IF pos > UPB text
THEN
FALSE
ELSE
text[ pos ] <= " "
FI
DO
pos +:= 1
OD;
text[ pos : ]
END; # trim leading whitespace #
# returns "text" with trailing non-printing characters removed #
PROC trim trailing whitespace = ( STRING text )STRING:
BEGIN
INT pos := UPB text;
WHILE
IF pos < LWB text
THEN
FALSE
ELSE
text[ pos ] <= " "
FI
DO
pos -:= 1
OD;
text[ : pos ]
END; # trim trailing whitespace #
# returns "text" with leading and trailing non-printing characters removed #
PROC trim whitespace = ( STRING text )STRING:
BEGIN
trim trailing whitespace( trim leading whitespace( text ) )
END; # trim whitespace #
main:(
STRING test = " leading and trailing spaces surrounded this text ";
print( ( "trim leading: """ + trim leading whitespace ( test ) + """", newline ) );
print( ( "trim trailing: """ + trim trailing whitespace( test ) + """", newline ) );
print( ( "trim both: """ + trim whitespace ( test ) + """", newline ) )
)
- Output:
trim leading: "leading and trailing spaces surrounded this text " trim trailing: " leading and trailing spaces surrounded this text" trim both: "leading and trailing spaces surrounded this text"
version 2
A single procedure that trims leading space, trailing space, or both.
#
string_trim
Trim leading and trailing whitespace from string.
@param str A string.
@return A string trimmed of leading and trailing white space.
#
PROC string_trim = (STRING str) STRING: (
INT i := 1, j := 0;
WHILE str[i] = blank DO
i +:= 1
OD;
WHILE str[UPB str - j] = blank DO
j +:= 1
OD;
str[i:UPB str - j]
);
test: (
IF string_trim(" foobar") /= "foobar" THEN
print(("string_trim(' foobar'): expected 'foobar'; actual: "
+ string_trim(" foobar"), newline)) FI;
IF string_trim("foobar ") /= "foobar" THEN
print(("string_trim('foobar '): expected 'foobar'; actual: "
+ string_trim("foobar "), newline)) FI;
IF string_trim(" foobar ") /= "foobar" THEN
print(("string_trim(' foobar '): expected 'foobar'; actual: "
+ string_trim(" foobar "), newline)) FI
)
Amazing Hopper
Amazing Hopper! Flavour "Jambo".
#include <jambo.h>
Main
Set stack 25
c = "\t\t\n \n Message to triming\t\n \t"
Printnl ("Original Message: [", c,"]")
Set '"\n\n\UL\ENFNatural syntax:\n\n"', then bold off, and underline off
Set '"Right trim: [", c', Do right trim; then set '"]"'
Set '"\nLeft trim: [", c', Do left trim; Set '"]"'
Set '"\nAll trim: [", c', Do trim, now set '"]"' and print with newline
Underline( Bold( "\n\nFormal syntax:\n\n" ))
Printnl ( "Right trim: [", Rtrim(c),\
"]\nLeft trim: [", Ltrim(c),\
"]\nAll trim: [",Trim(c),"]\n" )
End
- Output:
Original Message: [ Message to triming ] Natural syntax: Right trim: [ Message to triming] Left trim: [Message to triming ] All trim: [Message to triming] Formal syntax: Right trim: [ Message to triming] Left trim: [Message to triming ] All trim: [Message to triming]
AppleScript
use framework "Foundation" -- "OS X" Yosemite onwards, for NSRegularExpression
-- STRIP WHITESPACE ----------------------------------------------------------
-- isSpace :: Char -> Bool
on isSpace(c)
((length of c) = 1) and regexTest("\\s", c)
end isSpace
-- stripStart :: Text -> Text
on stripStart(s)
dropWhile(isSpace, s) as text
end stripStart
-- stripEnd :: Text -> Text
on stripEnd(s)
dropWhileEnd(isSpace, s) as text
end stripEnd
-- strip :: Text -> Text
on strip(s)
dropAround(isSpace, s) as text
end strip
-- TEST ----------------------------------------------------------------------
on run
set strText to " \t\t \n \r Much Ado About Nothing \t \n \r "
script arrowed
on |λ|(x)
"-->" & x & "<--"
end |λ|
end script
map(arrowed, [stripStart(strText), stripEnd(strText), strip(strText)])
-- {"-->Much Ado About Nothing
--
-- <--", "-->
--
-- Much Ado About Nothing<--", "-->Much Ado About Nothing<--"}
end run
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- dropAround :: (Char -> Bool) -> [a] -> [a]
on dropAround(p, xs)
dropWhile(p, dropWhileEnd(p, xs))
end dropAround
-- dropWhile :: (a -> Bool) -> [a] -> [a]
on dropWhile(p, xs)
tell mReturn(p)
set lng to length of xs
set i to 1
repeat while i ≤ lng and |λ|(item i of xs)
set i to i + 1
end repeat
end tell
if i ≤ lng then
items i thru lng of xs
else
{}
end if
end dropWhile
-- dropWhileEnd :: (a -> Bool) -> [a] -> [a]
on dropWhileEnd(p, xs)
tell mReturn(p)
set i to length of xs
repeat while i > 0 and |λ|(item i of xs)
set i to i - 1
end repeat
end tell
if i > 0 then
items 1 thru i of xs
else
{}
end if
end dropWhileEnd
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- regexTest :: RegexPattern -> String -> Bool
on regexTest(strRegex, str)
set ca to current application
set oString to ca's NSString's stringWithString:str
((ca's NSRegularExpression's regularExpressionWithPattern:strRegex ¬
options:((ca's NSRegularExpressionAnchorsMatchLines as integer)) ¬
|error|:(missing value))'s firstMatchInString:oString options:0 ¬
range:{location:0, |length|:oString's |length|()}) is not missing value
end regexTest
- Output:
{"-->Much Ado About Nothing
<--", "-->
Much Ado About Nothing<--", "-->Much Ado About Nothing<--"}
Arturo
str: " Hello World "
print [pad "strip all:" 15 ">" strip str "<"]
print [pad "strip leading:" 15 ">" strip.start str "<"]
print [pad "strip trailing:" 15 ">" strip.end str "<"]
- Output:
strip all: > Hello World < strip leading: > Hello World < strip trailing: > Hello World <
AutoHotkey
AutoHotkey_L and v2 contain a Trim function
string := " abc "
MsgBox % clipboard := "<" LTrim(string) ">`n<" RTrim(string) ">`n<" . Trim(string) ">"
- Output:
<abc > < abc> <abc>
AWK
- [:graph:] is a POSIX character class for characters that are both printable and visible. The following strips the entire non-[:graph:] character set. A simplified version for the more common task of removing only spaces and tabs follows it.
function trimleft(str ,c, out, arr) {
c = split(str, arr, "")
for ( i = match(str, /[[:graph:]]/); i <= c; i++)
out = out arr[i]
return out
}
function reverse(str ,n, tmp, j, out) {
n = split(str, tmp, "")
for (j = n; j > 0; j--)
out = out tmp[j]
return out
}
function trimright(str) {
return reverse(trimleft(reverse(str)))
}
function trim(str) {
return trimright(trimleft(str))
}
BEGIN {
str = " \x0B\t\r\n \xA0 Hellö \xA0\x0B\t\r\n "
print "string = |" str "|"
print "left = |" trimleft(str) "|"
print "right = |" trimright(str) "|"
print "both = |" trim(str) "|"
}
- Output from 26 May 2015:
string = | ▒ Hellö ▒ | left = |Hellö ▒ | right = | ▒ Hellö| both = |Hellö|
Simplified for removing [:blank:] (spaces and tabs) or [:space:] (for [ \t\r\n\v\f] ). This method does not work using [:graph:]
function trim(str) {
gsub(/^[[:blank:]]+/,"", str) # Remove leading
gsub(/[[:blank:]]+$/,"", str) # Remove trailing
gsub(/^[[:blank:]]+|[[:blank:]]+$/, "", str) # Remove both
return str;
}
BaCon
'---Remove leading whitespace
PRINT CHOP$(" String with spaces "," ",1)
'---Remove trailing whitespace
PRINT CHOP$(" String with spaces "," ",2)
'---Remove both leading and trailing whitespace
PRINT CHOP$(" String with spaces "," ",0)
BASIC
mystring$=ltrim(mystring$) ' remove leading whitespace
mystring$=rtrim(mystring$) ' remove trailing whitespace
mystring$=ltrim(rtrim(mystring$)) ' remove both leading and trailing whitespace
BASIC256
s$ = " \tRosetta Code \v\f\r\n"
print ltrim(s$) # remove leading whitespace
print rtrim(s$) # remove trailing whitespace
print trim(s$) # remove both leading and trailing whitespace
BBC BASIC
REM Remove leading whitespace:
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE
REM Remove trailing whitespace:
WHILE ASC(RIGHT$(A$))<=32 A$ = LEFT$(A$) : ENDWHILE
REM Remove both leading and trailing whitespace:
WHILE ASC(A$)<=32 A$ = MID$(A$,2) : ENDWHILE
WHILE ASC(RIGHT$(A$))<=32 A$ = LEFT$(A$) : ENDWHILE
BQN
The following program uses an AND-scan (a well known idiom for leading characters) to find the leading whitespace. The whitespace characters are then filtered out from those locations.
Composing the leading and trailing idioms removes whitespace from both sides.
ws ← @+⟨9, 10, 11, 12, 13, 32, 133, 160,
5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198,
8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287,
12288⟩
Lead ← (¬·∧`∊⟜ws)⊸/
Trail ← (¬·∧`⌾⌽∊⟜ws)⊸/
•Show Lead " fs df"
•Show Trail "fdsf asd "
•Show Lead∘Trail " white space "
"fs df"
"fdsf asd"
"white space"
Bracmat
Greedy pattern matching is not Bracmat's strongest field. So instead of a pattern that globs all white space characters, we have a pattern that finds the first non-whitespace character. That character, and the remainder of the subject string, constitute a left trimmed string. To do a right trim, we reverse the string, do a left trim and reverse back.
( ( ltrim
= s
. @( !arg
: ?
( ( %@
: ~( " "
| \a
| \b
| \n
| \r
| \t
| \v
)
)
?
: ?s
)
)
& !s
)
& (rtrim=.rev$(ltrim$(rev$!arg)))
& (trim=.rev$(ltrim$(rev$(ltrim$!arg))))
& (string=" \a Hear
the sound?
\v
\r
")
& out$(str$("Input:[" !string "]"))
& out$(str$("ltrim:[" ltrim$!string "]"))
& out$(str$("rtrim:[" rtrim$!string "]"))
& out$(str$("trim :[" trim$!string "]"))
&
);
Output (Notice the effect of the ancient \a (alarm) and \v (vertical tab)):
Input:[ Hear the sound? ♂ ] ltrim:[Hear the sound? ♂ ] rtrim:[ Hear the sound?] trim :[Hear the sound?]
Burlesque
blsq ) " this is a string "t[
"this is a string "
blsq ) " this is a string "t]
" this is a string"
blsq ) " this is a string "tt
"this is a string"
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char *rtrim(const char *s)
{
while( isspace(*s) || !isprint(*s) ) ++s;
return strdup(s);
}
char *ltrim(const char *s)
{
char *r = strdup(s);
if (r != NULL)
{
char *fr = r + strlen(s) - 1;
while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
*++fr = 0;
}
return r;
}
char *trim(const char *s)
{
char *r = rtrim(s);
char *f = ltrim(r);
free(r);
return f;
}
const char *a = " this is a string ";
int main()
{
char *b = rtrim(a);
char *c = ltrim(a);
char *d = trim(a);
printf("'%s'\n'%s'\n'%s'\n", b, c, d);
free(b);
free(c);
free(d);
return 0;
}
Gadget
Gadget C-library: Gadget C-library in Github
#include <gadget/gadget.h>
LIB_GADGET_START
Main
String r,s,t;
Stack{
Store ( r, Trim_r (Upper(" \n\t este mensaje será modificado " )) )
Store ( s, Trim_l (Reverse(Upper(" \n\t este mensaje será odasrever " ))) )
Store ( t, Trim (Upper(" \n\t este mensaje será modificado " )) )
}Stack_off
Print "Right Trim = [%s]\nLeft Trim = [%s]\nAll Trim = [%s]\n",r,s,t;
Free secure r,t, s;
End
- Output:
Right Trim = [ ESTE MENSAJE SERÁ MODIFICADO] Left Trim = [REVERSADO ÁRES EJASNEM ETSE ] All Trim = [ESTE MENSAJE SERÁ MODIFICADO]
C#
using System;
public class TrimExample
{
public static void Main(String[] args)
{
const string toTrim = " Trim me ";
Console.WriteLine(Wrap(toTrim.TrimStart()));
Console.WriteLine(Wrap(toTrim.TrimEnd()));
Console.WriteLine(Wrap(toTrim.Trim()));
}
private static string Wrap(string s)
{
return "'" + s + "'";
}
}
- Output:
'Trim me ' ' Trim me' 'Trim me'
C++
#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>
int main( ) {
std::string testphrase( " There are unwanted blanks here! " ) ;
std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
std::cout << "The test phrase is :" << testphrase << "\n" ;
std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
boost::trim( testphrase ) ;
std::cout << "Trimmed on both sides :" << testphrase << "\n" ;
return 0 ;
}
- Output:
The test phrase is : There are unwanted blanks here! Trimmed on the left side :There are unwanted blanks here! Trimmed on the right side : There are unwanted blanks here! Trimmed on both sides :There are unwanted blanks here!
Clojure
(use 'clojure.string)
(triml " my string ")
=> "my string "
(trimr " my string ")
=> " my string"
(trim " \t\r\n my string \t\r\n ")
=> "my string"
COBOL
DISPLAY "'" FUNCTION TRIM(str, LEADING) "'"
DISPLAY "'" FUNCTION TRIM(str, TRAILING) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
DISPLAY "'" FUNCTION TRIML(str) "'"
DISPLAY "'" FUNCTION TRIMR(str) "'"
DISPLAY "'" FUNCTION TRIM(str) "'"
Common Lisp
; Common whitespace characters
(defvar *whitespace* '(#\Space #\Newline #\Tab))
(defvar str " foo bar baz ")
(string-trim *whitespace* str)
; -> "foo bar baz"
(string-left-trim *whitespace* str)
; -> "foo bar baz "
(string-right-trim *whitespace* str)
; -> " foo bar baz"
; Whitespace characters defined by Unicode for
; implementations which support it (e.g. CLISP, SBCL).
; (see http://www.unicode.org/Public/UCD/latest/ucd/PropList.txt)
(defvar *unicode-whitespace*
'(#\u0009 #\u000a #\u000b #\u000c #\u000d
#\u0020 #\u0085 #\u00a0 #\u1680 #\u2000
#\u2001 #\u2002 #\u2003 #\u2004 #\u2005
#\u2006 #\u2007 #\u2008 #\u2009 #\u200a
#\u2028 #\u2029 #\u202f #\u205f #\u3000))
(defvar unicode-str
(format nil "~C~Cfoo~Cbar~Cbaz~C~C"
#\u2000 #\u2003 #\u0020 #\u00a0 #\u0009 #\u202f))
(string-trim *unicode-whitespace* unicode-str)
; -> "foo bar baz"
(string-left-trim *unicode-whitespace* unicode-str)
; -> "foo bar baz "
(string-right-trim *unicode-whitespace* unicode-str)
; -> " foo bar baz"
Crystal
def strip_whitepace(s)
puts s.lstrip()
puts s.rstrip()
puts s.strip()
end
strip_whitepace("\t hello \t")
# => hello
# => hello
# => hello
D
import std.stdio, std.string;
void main() {
auto s = " \t \r \n String with spaces \t \r \n ";
assert(s.stripLeft() == "String with spaces \t \r \n ");
assert(s.stripRight() == " \t \r \n String with spaces");
assert(s.strip() == "String with spaces");
}
Delphi/Pascal
program StripWhitespace;
{$APPTYPE CONSOLE}
uses SysUtils;
const
TEST_STRING = ' String with spaces ';
begin
Writeln('"' + TEST_STRING + '"');
Writeln('"' + TrimLeft(TEST_STRING) + '"');
Writeln('"' + TrimRight(TEST_STRING) + '"');
Writeln('"' + Trim(TEST_STRING) + '"');
end.
DWScript
const TEST_STRING = ' String with spaces ';
PrintLn('"' + TEST_STRING + '"');
PrintLn('"' + TrimLeft(TEST_STRING) + '"');
PrintLn('"' + TrimRight(TEST_STRING) + '"');
PrintLn('"' + Trim(TEST_STRING) + '"');
EasyLang
func iswhite c$ .
if c$ = " " or c$ = "\t" or c$ = "\n"
return 1
.
.
func$ strip s$ top tail .
a = 1
if top = 1
repeat
c$ = substr s$ a 1
until iswhite c$ = 0
a += 1
.
.
b = len s$
if tail = 1
repeat
c$ = substr s$ b 1
until iswhite c$ = 0
b -= 1
.
.
return substr s$ a (b - a + 1)
.
print strip " Hello world " 1 1 & "."
print strip " Hello world " 0 1 & "."
print strip " Hello world " 1 1 & "."
EchoLisp
(define witt
" The limits of my world are the limits of my langage. ")
(string->html (string-trim witt))
→ "The limits of my world are the limits of my langage."
(string->html (string-trim-left witt))
→ "The limits of my world are the limits of my langage. "
(string->html (string-trim-right witt))
→ " The limits of my world are the limits of my langage."
Elena
ELENA 4.x :
import extensions;
public program()
{
var toTrim := " Trim me ";
console.printLine("'", toTrim.trimLeft(),"'");
console.printLine("'", toTrim.trimRight(),"'");
console.printLine("'", toTrim.trim(),"'");
}
- Output:
'Trim me ' ' Trim me' 'Trim me'
Elixir
str = "\n \t foo \n\t bar \t \n"
IO.inspect String.strip(str)
IO.inspect String.rstrip(str)
IO.inspect String.lstrip(str)
- Output:
"foo \n\t bar" "\n \t foo \n\t bar" "foo \n\t bar \t \n"
Emacs Lisp
As of Emacs 24.4, the subr-x.el library contains string trimming functions:
(string-trim-left " left center right ") ;=> "left center right "
(string-trim-right " left center right ") ;=> " left center right"
(string-trim " left center right ") ;=> "left center right"
This can be emulated by using regex replacement:
(defun string-trim-left (str)
(replace-regexp-in-string "^[ \t\r\n]*" "" str))
(defun string-trim-right (str)
(replace-regexp-in-string "[ \t\r\n]*$" "" str))
(defun string-trim (str)
(string-trim-left (string-trim-right str)))
Erlang
% Implemented by Arjun Sunel
1> string:strip(" Hello World! ", left). %remove leading whitespaces
"Hello World! "
2> string:strip(" Hello World! ", right). % remove trailing whitespaces
" Hello World!"
3> string:strip(" Hello World! ", both). % remove both leading and trailing whitespace
"Hello World!"
Euphoria
A string (sequence) 'A B C' is surrounded by unwanted characters including spaces. The trim() function Trims "all items in the supplied set from both the left end (head/start) and right end (tail/end) of a sequence." It's part of the standard library, std/text.e .
Special characters in a string literal are typed 'escaped' with a '\' followed by one character. Other special characters are written using escaped hexadecimal , example : \x04 to represent hexadecimal ascii 04 or \u2A7C for 4-digit UTF, or more than two digit ascii characters : \u0127.
include std/console.e
include std/text.e
sequence removables = " \t\n\r\x05\u0234\" "
sequence extraSeq = " \x05\r \" A B C \n \t\t \u0234 \r\r \x05 "
extraSeq = trim(extraSeq,removables) --the work is done by the trim function
--only output programming next :
printf(1, "String Trimmed is now: %s \r\n", {extraSeq} ) --print the resulting string to screen
for i = 1 to length(extraSeq) do --loop over each character in the sequence.
printf(1, "String element %d", i) --to look at more detail,
printf(1, " : %d\r\n", extraSeq[i])--print integer values(ascii) of the string.
end for
any_key()
Output:
String Trimmed is now: A B C String element 1 : 65 String element 2 : 32 String element 3 : 32 String element 4 : 66 String element 5 : 32 String element 6 : 32 String element 7 : 67 Press Any Key to continue...
F#
[<EntryPoint>]
let main args =
printfn "%A" (args.[0].TrimStart())
printfn "%A" (args.[0].TrimEnd())
printfn "%A" (args.[0].Trim())
0
>rosetta " a string " "a string " " a string" "a string"
Factor
USE: unicode
" test string " [ blank? ] trim ! leading and trailing
" test string " [ blank? ] trim-head ! only leading
" test string " [ blank? ] trim-tail ! only trailing
Forth
Modern Forth advocates the use of "stack strings". These are simply the memory address of the text on the stack followed by the length of the string on the stack. The advantage of using stack strings is that string manipulation is done without resorting to copying the strings to temporary memory. This makes string manipulation very fast. The values left on the stack are suitable as input arguments for further processing so function concatenation is possible to create more complex functions.
After the string processing is completed on the stack the program can print or copy back to memory as required.
: -leading ( addr len -- addr' len' ) \ called "minus-leading"
begin
over c@ bl = \ fetch character at addr, test if blank (space)
while
\ cut 1 leading character by incrementing address & decrementing length
1 /string \ "cut-string"
repeat ;
The counterpart function "-trailing" is normally part of a standard forth system, so by concatenating both we create a "STRIP" function.
: strip ( addr len -- addr' len') -leading -trailing ;
Test at the Forth console
: mystring s" Trim this string. " ; mstring type Trim this string. ok mstring -leading type Trim this string. ok mystring -trailing type Trim this string. ok mytring strip type Trim this string. ok
Fortran
The problem is that Fortran does not offer a "string" type, which is to say a store of characters plus a current length. Leaving aside later Fortrans where one can create a data type and associated operations, one must deal with CHARACTER style items, though they were not available in early Fortran. Thus, CHARACTER*(60) TEXT
creates a variable capable of storing sixty characters and there is no "varying" attribute as in pl/i to signify a current length as well. The intrinsic function LEN(TEXT) returns 60, so it is really the "size" function. Functions can return CHARACTER types, but, only of a specified size, fixed at compile time. It is common then to have many trailing spaces in such variables. An expression TEXT(start:stop)
means select characters start to stop of TEXT.
A function LEN_TRIM(x) may be supplied, returning the index of the last non-blank character. And only of spaces: tabs and suchlike are not blanks. Thus, to remove trailing spaces, TEXT(1:LEN_TRIM(TEXT))
would do the trick. There may also be available an intrinsic function TRIM, which removes leading and trailing spaces, thus TRIM(TEXT)
and which does so for any length of TEXT and any length of result, unlike user-written functions that have a fixed result length.
Otherwise, one must write integer functions such as ISTNB(x) and LSTNB(x) to return the first and last non-blank character of x and use them in something like TEXT(ISTNB(TEXT):LSTNB(TEXT))
wherever one wants the trimmed content of TEXT. Correspondingly TEXT(ISTNB(TEXT):LEN(TEXT))
to omit leading spaces and TEXT(1:LSTNB(TEXT))
to omit trailing spaces. Such user-written functions can make their own choices about tabs and any other special characters.
FreeBASIC
' FB 1.05.0 Win64
Const whitespace = !" \t\n\v\f\r"
Dim s As String = !" \tRosetta Code \v\f\r\n"
Dim s1 As String = LTrim (s, Any whitespace)
Dim s2 As String = RTrim (s, Any whitespace)
Dim s3 As String = Trim (s, Any whitespace)
' Under Windows console :
' "vertical tab" displays as ♂
' "form feed" displays as ♀
' the other whitespace characters do what it says on the tin
Print "Untrimmed" , "=> "; s
Print "Left Trimmed" , "=> "; s1
Print "Right Trimmed" , "=> "; s2
Print "Fully Trimmed" , "=> "; s3
Print
Print "Untrimmed" , "=> Length = "; Len(s)
Print "Left trimmed" , "=> Length = "; Len(s1)
Print "Right trimmed" , "=> Length = "; Len(s2)
Print "Fully trimmed" , "=> Length = "; Len(s3)
Sleep
- Output:
Untrimmed => Rosetta Code ♂♀ Left Trimmed => Rosetta Code ♂♀ Right Trimmed => Rosetta Code Fully Trimmed => Rosetta Code Untrimmed => Length = 20 Left trimmed => Length = 17 Right trimmed => Length = 15 Fully trimmed => Length = 12
FutureBasic
window 1
CFCharacterSetRef set, invSet
CFStringRef s, s1, s2, s3
NSUInteger index
CFRange range
set = fn CharacterSetWhitespaceAndNewlineSet
invSet = fn CharacterSetInvertedSet( set )
text ,,,,, 200
s = @" a string "// 5 leading spaces, 8 trailing spaces
print s, len(s)@" chars"
// left trim
index = 0
range = fn StringRangeOfCharacterFromSet( s, invSet )
if ( range.location != NSNotFound ) then index = range.location
s1 = fn StringSubstringFromIndex( s, index )
print s1, len(s1)@" chars"
// right trim
index = len(s)
range = fn StringRangeOfCharacterFromSetWithOptions( s, invSet, NSBackwardsSearch )
if ( range.location != NSNotFound ) then index = range.location + 1
s2 = fn StringSubstringToIndex( s, index )
print s2, len(s2)@" chars"
// trim
s3 = fn StringByTrimmingCharactersInSet( s, set )
print s3, len(s3)@" chars"
HandleEvents
- Output:
a string 21 chars a string 16 chars a string 13 chars a string 8 chars
Gambas
Click this link to run this code
Public Sub Main()
Dim sString As String = " Hello world! "
Print sString & "\tString length = " & Len(sString) & " - Original string"
Print LTrim(sString) & "\tString length = " & Len(LTrim(sString)) & " - String with leading whitespace removed"
Print RTrim(sString) & "\tString length = " & Len(RTrim(sString)) & " - String with trailing whitespace removed"
Print Trim(sString) & "\t\tString length = " & Len(Trim(sString)) & " - String with both leading and trailing whitespace removed"
End
Output:
Hello world! String length = 20 - Original string Hello world! String length = 16 - String with leading whitespace removed Hello world! String length = 16 - String with trailing whitespace removed Hello world! String length = 12 - String with both leading and trailing whitespace removed
Go
package main
import (
"fmt"
"strings"
"unicode"
)
var simple = `
simple `
func main() {
show("original", simple)
show("leading ws removed", strings.TrimLeftFunc(simple, unicode.IsSpace))
show("trailing ws removed", strings.TrimRightFunc(simple, unicode.IsSpace))
// equivalent to strings.TrimFunc(simple, unicode.IsSpace)
show("both removed", strings.TrimSpace(simple))
}
func show(label, str string) {
fmt.Printf("%s: |%s| %v\n", label, str, []rune(str))
}
Example text is shows a leading linefeed and tab, and three trailing spaces. The code uses the Unicode definition of whitespace. Other defintions could be implemented with a custom function given to TrimXFunc.
Output below shows the text surrounded by vertical bars to show the extent of whitespace, followed by a list of the character values in the string, to show exactly what whitespace is present.
original: | simple | [10 9 115 105 109 112 108 101 32 32 32] leading ws removed: |simple | [115 105 109 112 108 101 32 32 32] trailing ws removed: | simple| [10 9 115 105 109 112 108 101] both removed: |simple| [115 105 109 112 108 101]
Groovy
Solution uses StringUtils class from Apache Commons "Lang" library:
//Grape setup to get library
@Grab('org.apache.commons:commons-lang3:3.0.1')
import static org.apache.commons.lang3.StringUtils.*
def abc = '\r\n\t abc \r\n\t'
def printTest = {
println ('|' + it + '|')
}
println 'Unstripped\n------------'
printTest abc
println '============\n\nStripped\n------------'
printTest strip(abc)
println '============\n\nLeft Stripped\n------------'
printTest stripStart(abc, null)
println '============\n\nRight Stripped\n------------'
printTest stripEnd(abc, null)
println '============'
- Output:
Unstripped ------------ | abc | ============ Stripped ------------ |abc| ============ Left Stripped ------------ |abc | ============ Right Stripped ------------ | abc| ============
Haskell
import Data.Char (isSpace)
import Data.List (dropWhileEnd)
trimLeft :: String -> String
trimLeft = dropWhile isSpace
trimRight :: String -> String
trimRight = dropWhileEnd isSpace
trim :: String -> String
trim = trimLeft . trimRight
Icon and Unicon
This solution takes the phrase "other such characters that have no corresponding graphical representation" quite literallly.
procedure main()
unp := &cset[1+:32]++' \t'++&cset[127:0] # all 'unprintable' chars
s := " Hello, people of earth! "
write("Original: '",s,"'")
write("leading trim: '",reverse(trim(reverse(s),unp)),"'")
write("trailing trim: '",trim(s,unp),"'")
write("full trim: '",reverse(trim(reverse(trim(s,unp)),unp)),"'")
end
- Sample run:
->trim Original: ' Hello, people of earth! ' leading trim: 'Hello, people of earth! ' trailing trim: ' Hello, people of earth!' full trim: 'Hello, people of earth!' ->
J
Note: The quote
verb is only used to enclose the resulting string in single quotes so the beginning and end of the new string are visible.
require 'strings' NB. the strings library is automatically loaded in versions from J7 on
quote dlb ' String with spaces ' NB. delete leading blanks
'String with spaces '
quote dtb ' String with spaces ' NB. delete trailing blanks
' String with spaces'
quote dltb ' String with spaces ' NB. delete leading and trailing blanks
'String with spaces'
In addition deb
(delete extraneous blanks) will trim both leading and trailing blanks as well as replace consecutive spaces within the string with a single space.
quote deb ' String with spaces ' NB. delete extraneous blanks
'String with spaces'
These existing definitions can be easily amended to include whitespace other than spaces if desired.
whpsc=: ' ',TAB NB. define whitespace as desired
dlws=: }.~ (e.&whpsc i. 0:) NB. delete leading whitespace (spaces and tabs)
dtws=: #~ ([: +./\. -.@:e.&whpsc) NB. delete trailing whitespace
dltws=: #~ ([: (+./\ *. +./\.) -.@:e.&whpsc) NB. delete leading & trailing whitespace
dews=: #~ (+. (1: |. (> </\)))@(-.@:e.&whpsc) NB. delete extraneous whitespace
Java
Java has the trim and strip operations. Both will achieve a similar result.
The trim
method works on Unicode values under 0x20. Whereas, strip will remove all Unicode white-space characters.
" abc".stripLeading()
"abc ".stripTrailing()
" abc ".strip()
" abc ".trim()
For more control over what characters should be removed, you could implement your own methods.
String removeLeading(String string, char[] characters) {
int index = 0;
for (char characterA : string.toCharArray()) {
for (char characterB : characters) {
if (characterA != characterB)
return string.substring(index);
}
index++;
}
return string;
}
String removeTrailing(String string, char[] characters) {
for (int index = string.length() - 1; index >= 0; index--) {
for (char character : characters) {
if (string.charAt(index) != character)
return string.substring(0, index + 1);
}
}
return string;
}
An alternate demonstration
Java offers String.trim
. However, this function only strips out ASCII control characters. As such it should generally be avoided for processing text.
Rather, the function Character.isWhitespace
should be used to strip out any Unicode whitespaces. Note that the Java implementation slightly deviates from the strict Unicode definition: For example, non-breaking spaces are not considered whitespace by Java. This is fine for stripping leading/trailing whitespaces, as a non-breaking space generally should not appear there, but is something to keep in mind.
Left trim and right trim taken from here.
public class Trims{
public static String ltrim(String s) {
int i = 0;
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
i++;
}
return s.substring(i);
}
public static String rtrim(String s) {
int i = s.length() - 1;
while (i > 0 && Character.isWhitespace(s.charAt(i))) {
i--;
}
return s.substring(0, i + 1);
}
public static String trim(String s) {
return rtrim(ltrim(s));
}
public static void main(String[] args) {
String s = " \t \r \n String with spaces \u2009 \t \r \n ";
System.out.printf("[%s]\n", ltrim(s));
System.out.printf("[%s]\n", rtrim(s));
System.out.printf("[%s]\n", trim(s));
}
}
Supplementary-correct version
Unicode *happens* to not have any whitespace characters outside of Basic Multilingual Plane (aka, they all fit inside a char
). However, this not something you should generally rely on, and should assume your strings contain characters in the Supplementary Planes. As such, instead of iterating using String.charAt
, prefer instead using String.codePointAt
and String.codePointBefore
, iterating over actual Unicode Code Points:
public static String ltrim(String s) {
int offset = 0;
while (offset < s.length()) {
int codePoint = s.codePointAt(offset);
if (!Character.isWhitespace(codePoint)) break;
offset += Character.charCount(codePoint);
}
return s.substring(offset);
}
public static String rtrim(String s) {
int offset = s.length();
while (offset > 0) {
int codePoint = s.codePointBefore(offset);
if (!Character.isWhitespace(codePoint)) break;
offset -= Character.charCount(codePoint);
}
return s.substring(0, offset);
}
JavaScript
{
let s = " \t String with spaces \t ";
// a future version of ECMAScript will have trimStart(). Some current
// implementations have trimLeft().
console.log("original: '" + s + "'");
console.log("trimmed left: '" + s.replace(/^\s+/,'') + "'");
// a future version of ECMAScript will have trimEnd(). Some current
// implementations have trimRight().
console.log("trimmed right: '" + s.replace(/\s+$/,'') + "'");
console.log("trimmed both: '" + s.trim() + "'");
}
- Output:
original: ' String with spaces ' trimmed left: 'String with spaces ' trimmed right: ' String with spaces' trimmed both: 'String with spaces'
Or, composing from generic primitives:
(() => {
'use strict';
// stripStart :: Text -> Text
let stripStart = s => dropWhile(isSpace, s);
// stripEnd :: Text -> Text
let stripEnd = s => dropWhileEnd(isSpace, s);
// strip :: Text -> Text
let strip = s => dropAround(isSpace, s);
// OR: let strip = s => s.trim();
// GENERIC FUNCTIONS
// dropAround :: (Char -> Bool) -> Text -> Text
let dropAround = (p, s) => dropWhile(p, dropWhileEnd(p, s));
// dropWhile :: (a -> Bool) -> [a] -> [a]
let dropWhile = (p, xs) => {
for (var i = 0, lng = xs.length;
(i < lng) && p(xs[i]); i++) {}
return xs.slice(i);
}
// dropWhileEnd :: (Char -> Bool) -> Text -> Text
let dropWhileEnd = (p, s) => {
for (var i = s.length; i-- && p(s[i]);) {}
return s.slice(0, i + 1);
}
// isSpace :: Char -> Bool
let isSpace = c => /\s/.test(c);
// show :: a -> String
let show = x => JSON.stringify(x, null, 2);
// TEST
let strText = " \t\t \n \r Much Ado About Nothing \t \n \r ";
return show([stripStart, stripEnd, strip]
.map(f => '-->' + f(strText) + '<--'));
})();
- Output:
[ "-->Much Ado About Nothing \t \n \r <--", "--> \t\t \n \r Much Ado About Nothing<--", "-->Much Ado About Nothing<--" ]
jq
Recent versions of jq (since July 2014) support PCRE regex operations; \p{_} character classes are also supported. The following accordingly uses \p{Cc} as that corresponds to ASCII 0x00–0x1F.
Notice that since jq strings are JSON strings, one must, for example, write "\\s" for the regex '\s'.
def lstrip: sub( "^[\\s\\p{Cc}]+"; "" );
def rstrip: sub( "[\\s\\p{Cc}]+$"; "" );
def strip: lstrip | rstrip;
Examples:
def demo:
"lstrip: \(lstrip)",
"rstrip: \(rstrip)",
"strip: \(strip)" ;
(" \t \r \n String with spaces \t \r \n ",
"� <- control A",
"\u0001 \u0002 <- ^A ^B"
) | demo
- Output:
$ jq -n -f Strip_whitespace_top_tail.jq
"lstrip: String with spaces \t \r \n "
"rstrip: \t \r \n String with spaces"
"strip: String with spaces"
"rstrip: \u0001 <- control A"
"strip: <- control A"
"lstrip: <- ^A ^B"
"rstrip: \u0001 \u0002 <- ^A ^B"
"strip: <- ^A ^B"
Jsish
Using echo-mode unitTest lines. Lines starting with semicolon are echoed and captured during test.
#!/usr/bin/env jsish
/* Strip whitespace from string, in Jsi */
var str = ' \n \t String with whitespace \t \n ';
;'Original';
;str;
;'Default trim characters are space, tab, newline, carriage return';
;'trimLeft, remove leading characters';
;str.trimLeft();
;'trimRight, remove trailing characters';
;str.trimRight();
;'trim, removes leading and trailing';
;str.trim();
/*
=!EXPECTSTART!=
'Original'
str ==>
String with whitespace
'Default trim characters are space, tab, newline, carriage return'
'trimLeft, remove leading characters'
str.trimLeft() ==> String with whitespace
'trimRight, remove trailing characters'
str.trimRight() ==>
String with whitespace
'trim, removes leading and trailing'
str.trim() ==> String with whitespace
=!EXPECTEND!=
*/
- Output:
prompt$ jsish -u stripWhitespace.jsi [PASS] stripWhitespace.jsi prompt$ jsish --U stripWhitespace.jsi 'Original' str ==> String with whitespace 'Default trim characters are space, tab, newline, carriage return' 'trimLeft, remove leading characters' str.trimLeft() ==> String with whitespace 'trimRight, remove trailing characters' str.trimRight() ==> String with whitespace 'trim, removes leading and trailing' str.trim() ==> String with whitespace
Julia
julia> s = " \t \r \n String with spaces \t \r \n "
" \t \r \n String with spaces \t \r \n "
julia> lstrip(s)
"String with spaces \t \r \n "
julia> rstrip(s)
" \t \r \n String with spaces"
julia> strip(s)
"String with spaces"
K
dlb: {((" "<x)?1)_x}
dtb: {(-(|" "<x)?1)_x}
dlb " this is a test "
"this is a test "
dtb " this is a test "
" this is a test"
dtb dlb " this is a test "
"this is a test"
dtb dlb "this is a test"
"this is a test"
tested with ngn/k
Kotlin
It is worth pointing out that Kotlin, unlike Java, has String.trimStart
and String.trimEnd
. More importantly though, Kotlin’s String.trim
actually trims whitespace as defined by Unicode, whereas Java’s just strips Unicode control characters.
Kotlin’s trim
family of functions also optionally accept a predicate, which can further customize the stripping behavior, if Unicode whitespace is not what is desired.
fun main(args: Array<String>) {
val s = " \tRosetta Code \r \u2009 \n"
println("Untrimmed => [$s]")
println("Left Trimmed => [${s.trimStart()}]")
println("Right Trimmed => [${s.trimEnd()}]")
println("Fully Trimmed => [${s.trim()}]")
}
- Output:
Untrimmed => [ Rosetta Code ] Left Trimmed => [Rosetta Code ] Right Trimmed => [ Rosetta Code] Fully Trimmed => [Rosetta Code]
Ksh
#!/bin/ksh
# Strip whitespace from a string/Top and tail
# # Variables:
#
str=${1:-" This is a test. "}
# # Functions:
#
# # Function _striphead(str) - Return str with leading while space stripped
#
function _striphead {
typeset _str ; _str="$1"
echo "${_str##*(\s)}"
}
# # Function _striptail(str) - Return str with trailing while space stripped
#
function _striptail {
typeset _str ; _str="$1"
while [[ ${_str} != ${_str%+(\s)} ]]; do
_str=${_str%+(\s)}
done
echo "${_str}"
}
######
# main #
######
printf ">%s< (Unstripped %d chars)\n" "${str}" ${#str}
sstr=$(_striphead "${str}")
printf ">%s< (Strip leading (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} ))
sstr=$(_striptail "${str}")
printf ">%s< (Strip trailing (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} ))
sstr=$(_striphead "$(_striptail "${str}")")
printf ">%s< (Strip both (%d chars))\n" "${sstr}" $(( ${#str}-${#sstr} ))
- Output:
> This is a test. < (Unstripped 19 chars)>This is a test. < (Strip leading (1 chars)) > This is a test.< (Strip trailing (3 chars))
>This is a test.< (Strip both (4 chars))
Lambdatalk
initial string : {def string ___String with "_" displaying space_____}
-> string ___String with "_" displaying space_____
trimLeft : {S.replace ^_+ by in {string}}
-> String with "_" displaying space_____
trimRight : {S.replace _+$ by in {string}}
-> ___String with "_" displaying space
trimBoth : {S.replace _+$ by in {S.replace ^_+ by in {string}}}
-> String with "_" displaying space
Lasso
Note that the example below uses the retarget operator ("&") to output as the trim method modifies the string in place.
The '>' and '<' strings have been included in this example to demonstrate the whitespace trim.
// String with leading whitespace removed
'>' + (' \t Hello')->trim& + '<'
// String with trailing whitespace removed
'>' + ('Hello \t ')->trim& + '<'
// String with both leading and trailing whitespace removed
'>' + (' \t Hello \t ')->trim& + '<'
Liberty BASIC
a$=" This is a test "
'LB TRIM$ removes characters with codes 0..31 as well as a space(code 32)
'So these versions of ltrim rtrim remove them too
'a$=" "+chr$(31)+"This is a test"+chr$(31)+" "
print "Source line"
print ">";a$;"<"
print "Strip left"
print ">";ltrim$(a$);"<"
print "Strip right"
print ">";rtrim$(a$);"<"
print "Strip both"
print ">";trim$(a$);"<"
end
function ltrim$(a$)
c$=trim$(a$+".")
ltrim$ = mid$(c$, 1, len(c$)-1)
end function
function rtrim$(a$)
c$=trim$("."+a$)
rtrim$ = mid$(c$, 2)
end function
Logtalk
Using atoms for representing strings and assuming an ASCII text encoding:
:- object(whitespace).
:- public(trim/4).
trim(String, TrimLeft, TrimRight, TrimBoth) :-
trim_left(String, TrimLeft),
trim_right(String, TrimRight),
trim_right(TrimLeft, TrimBoth).
trim_left(String, TrimLeft) :-
atom_codes(String, Codes),
trim(Codes, TrimCodes),
atom_codes(TrimLeft, TrimCodes).
trim_right(String, TrimRight) :-
atom_codes(String, Codes),
list::reverse(Codes, ReverseCodes),
trim(ReverseCodes, ReverseTrimCodes),
list::reverse(ReverseTrimCodes, TrimCodes),
atom_codes(TrimRight, TrimCodes).
trim([], []).
trim([InCode| InCodes], OutCodes) :-
( InCode =< 32 ->
trim(InCodes, OutCodes)
; OutCodes = [InCode| InCodes]
).
:- end_object.
Sample output:
| ?- whitespace::trim('\n\t Rosetta Code \t\n', TrimLeft, TrimRight, TrimBoth).
TrimLeft = 'Rosetta Code \t\n',
TrimRight = '\n\t Rosetta Code',
TrimBoth = 'Rosetta Code'
yes
Lua
str = " \t \r \n String with spaces \t \r \n "
print( string.format( "Leading whitespace removed: %s", str:match( "^%s*(.+)" ) ) )
print( string.format( "Trailing whitespace removed: %s", str:match( "(.-)%s*$" ) ) )
print( string.format( "Leading and trailing whitespace removed: %s", str:match( "^%s*(.-)%s*$" ) ) )
M2000 Interpreter
Module CheckIt {
filter$=chr$(0)
for i=1 to 31:filter$+=chr$(i):next
a$=chr$(9)+" There are unwanted blanks here! "+chr$(9)
a$=filter$(a$,filter$) ' exclude non printable characters
\\ string encoded as UTF16LE
Print Len(a$)=39
Print(ltrim$(a$))
Print(rtrim$(a$))
Print(trim$(a$))
\\ string encoded as ANSI base to Locale
oldlocale=Locale
Locale 1033
a$=str$(a$)
Print Len(a$)=19.5 ' unit for length is a word (2 bytes), so 19.5 means 19.5*2 bytes= 39 bytes
PrintAnsi(ltrim$(a$ as byte))
PrintAnsi(rtrim$(a$ as byte))
PrintAnsi(trim$(a$ as byte))
Locale oldlocale
Sub Print(a$)
Print "*"+a$+"*"
End Sub
Sub PrintAnsi(a$)
Print "*"+chr$(a$)+"*"
End Sub
}
Checkit
- Output:
True *There are unwanted blanks here! * * There are unwanted blanks here!* *There are unwanted blanks here!* True *There are unwanted blanks here! * * There are unwanted blanks here!* *There are unwanted blanks here!*
Maple
str := " \t \r \n String with spaces \t \r \n ";
with(StringTools):
TrimLeft(str);
TrimRight(str);
Trim(str);
Mathematica/Wolfram Language
StringTrim[" \n\t string with spaces \n \t "]
MATLAB / Octave
% remove trailing whitespaces
str = str(1:find(~isspace(str),1,'last'));
% remove leading whitespaces
str = str(find(~isspace(str),1):end);
% removes leading and trailing whitespaces, vectorized version
f = ~isspace(str);
str = str(find(f,1,'first'):find(f,1,'last');
% a built-in function, removes leading and trailing whitespaces
str = strtrim(str);
Mercury
:- module top_and_tail.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module list, string.
main(!IO) :-
TestPhrase = "\t\r\n String with spaces \t\r\n ",
io.format("leading ws removed: %s\n", [s(lstrip(TestPhrase))], !IO),
io.format("trailing ws removed: %s\n", [s(rstrip(TestPhrase))], !IO),
io.format("both removed: %s\b", [s(strip(TestPhrase))], !IO).
Nemerle
def str = "\t\n\t A string with\nwhitespace\n\n\t ";
WriteLine(str.TrimStart());
WriteLine(str.TrimEnd());
WriteLine(str.Trim()); // both ends at once, of course, internal whitespace is preserved in all 3
NetRexx
NetRexx provides a strip() method which can be used to remove all of a single character from the head and/or tail of a string. To remove all whitespace characters from a string requires a little more work:
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method stripWhitespace(sstring, soption = 'BOTH') public static
wsChars = getWhitspaceCharacterString()
po1 = sstring.verify(wsChars)
if po1 = 0 then do
sstring = ''
end
else do
po2 = sstring.length - (sstring.reverse().verify(wsChars) - 1) + 1
ss = sstring
parse ss sl =(po1) sm =(po2) st
if po1 <= 1 then sl = ''
soption = soption.upper()
select
when 'BOTH'.abbrev(soption, 1) then sstring = sm
when 'LEADING'.abbrev(soption, 1) then sstring = sm || st
when 'TRAILING'.abbrev(soption, 1) then sstring = sl || sm
otherwise sstring = sm
end
end
return sstring
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* Create an array containing a useful subset of unicode whitespace characters
*
* @return an array of unicode whitespace characters
* @see http://www.fileformat.info/info/unicode/category/index.htm
*/
method getWhitspaceCharacters() public static returns Rexx[]
wsChars = [ -
/* LINE SEPARATOR [Zi] */ '\u2028', /* PARAGRAPH SEPARATOR [Zp] */ '\u2029', -
/* SPACE [Zs] */ '\u0020', /* NO-BREAK SPACE [Zs] */ '\u00A0', -
/* OGHAM SPACE MARK [Zs] */ '\u1680', /* MONGOLIAN VOWEL SEPARATOR [Zs] */ '\u180E', -
/* EN QUAD [Zs] */ '\u2000', /* EM QUAD [Zs] */ '\u2001', -
/* EN SPACE [Zs] */ '\u2002', /* EM SPACE [Zs] */ '\u2003', -
/* THREE-PER-EM SPACE [Zs] */ '\u2004', /* FOUR-PER-EM SPACE [Zs] */ '\u2005', -
/* SIX-PER-EM SPACE [Zs] */ '\u2006', /* FIGURE SPACE [Zs] */ '\u2007', -
/* PUNCTUATION SPACE [Zs] */ '\u2008', /* THIN SPACE [Zs] */ '\u2009', -
/* HAIR SPACE [Zs] */ '\u200A', /* NARROW NO-BREAK SPACE [Zs] */ '\u202F', -
/* MEDIUM MATHEMATICAL SPACE [Zs] */ '\u3000', /* IDIOGRAPHIC SPACE [Zs] */ '\u205F', -
/* BACKSPACE [Cc] */ '\u0008', /* CHARACTER TABULATION, HT [Cc] */ '\u0009', -
/* LINE FEED (LF) [Cc] */ '\u000A', /* LINE TABULATION (VT) [Cc] */ '\u000B', -
/* FORM FEED (FF) [Cc] */ '\u000C', /* CARRIAGE RETURN (CR) [Cc] */ '\u000D', -
/* INFORMATION SEPARATOR FOUR [Cc] */ '\u001C', /* INFORMATION SEPARATOR THREE [Cc] */ '\u001D', -
/* INFORMATION SEPARATOR TWO [Cc] */ '\u001E', /* INFORMATION SEPARATOR ONE [Cc] */ '\u001F', -
/* NEXT LINE (NEL) [Cc] */ '\u0085', -
/* ZERO WIDTH SPACE [Cf] */ '\u200B', /* ZERO WIDTH NO-BREAK SPACE [Cf] */ '\uFEFF' -
]
return wsChars
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getWhitspaceCharacterString() public static returns Rexx
wsCharstring = ''
loop wsChar over getWhitspaceCharacters()
wsCharstring = wsCharstring || wsChar
end wsChar
return wsCharstring
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
sstrings = [ -
' \u0020 \u0009 \u000D\r \n \u2029\uFEFF1 String with white space. \t \r \n \u1680 ', -
' \t 2 String with white space. \t ', -
'3 String with white space. \t', -
' \t 4 String with white space.', -
'5 String with white space.', -
'\u0020\u0009\u2029\uFEFF\u1680\u2006', -
' ', -
'' -
]
loop sstringO over sstrings
sstringL = stripWhitespace(sstringO, 'l')
sstringT = stripWhitespace(sstringO, 't')
sstringB = stripWhitespace(sstringO)
say ' Original string ['sstringO']'
say ' strip leading ['sstringL']'
say ' strip trailing ['sstringT']'
say ' strip both ['sstringB']'
say
end sstringO
return
Output:
Original string [ 1 String with white space. ] strip leading [1 String with white space. ] strip trailing [ 1 String with white space.] strip both [1 String with white space.] Original string [ 2 String with white space. ] strip leading [2 String with white space. ] strip trailing [ 2 String with white space.] strip both [2 String with white space.] Original string [3 String with white space. ] strip leading [3 String with white space. ] strip trailing [3 String with white space.] strip both [3 String with white space.] Original string [ 4 String with white space.] strip leading [4 String with white space.] strip trailing [ 4 String with white space.] strip both [4 String with white space.] Original string [5 String with white space.] strip leading [5 String with white space.] strip trailing [5 String with white space.] strip both [5 String with white space.] Original string [ ] strip leading [] strip trailing [] strip both [] Original string [ ] strip leading [] strip trailing [] strip both [] Original string [] strip leading [] strip trailing [] strip both []
NewLISP
(setq str " this is a string ")
;; trim leading blanks
(trim str " " "")
;; trim trailing blanks
(trim str "" " ")
;; trim both leading and trailing blanks
(trim str)
Nim
import strutils
let s = "\t \v \r String with spaces \n \t \f"
echo "“", s, "”"
echo("*** Stripped of leading spaces ***")
echo "“", s.strip(trailing = false), "”"
echo("*** Stripped of trailing spaces ***")
echo "“", s.strip(leading = false), "”"
echo("*** Stripped of leading and trailing spaces ***")
echo "“", s.strip(), "”"
- Output:
“ String with spaces ” *** Stripped of leading spaces *** “String with spaces ” *** Stripped of trailing spaces *** “ String with spaces” *** Stripped of leading and trailing spaces *** “String with spaces”
Oberon-2
Oxford Oberon-2
MODULE Trim;
IMPORT Out,Strings,SYSTEM;
CONST
(* whitespaces *)
HT = 09X; VT = 0BX; FF = 0CX; GS = 1DX; US = 1FX;
LF = 0AX; CR = 0DX; FS = 1CX; RS = 1EX; SPC = 20X;
PROCEDURE LTrim(VAR s: ARRAY OF CHAR);
VAR
j : INTEGER;
BEGIN
j := 0;
WHILE (s[j] = HT) OR (s[j] = LF) OR (s[j] = VT) OR (s[j] = CR) OR
(s[j] = FF) OR (s[j] = FS) OR (s[j] = FS) OR (s[j] = GS) OR
(s[j] = RS) OR (s[j] = US) OR (s[j] = SPC) DO INC(j) END;
SYSTEM.MOVE(SYSTEM.ADR(s[j]),SYSTEM.ADR(s[0]),LEN(s) - j);
END LTrim;
PROCEDURE RTrim(VAR s: ARRAY OF CHAR);
VAR
j : INTEGER;
BEGIN
j := LEN(s) - 1;
WHILE (j >= 0) & (s[j] = 0X) DO DEC(j) END;
WHILE (j >= 0) & ((s[j] = HT) OR (s[j] = LF) OR (s[j] = VT) OR (s[j] = CR) OR
(s[j] = FF) OR (s[j] = FS) OR (s[j] = FS) OR (s[j] = GS) OR
(s[j] = RS) OR (s[j] = US) OR (s[j] = SPC)) DO
s[j] := 0X;
DEC(j)
END
END RTrim;
PROCEDURE Trim(VAR s: ARRAY OF CHAR);
BEGIN
LTrim(s);
RTrim(s)
END Trim;
VAR
s: ARRAY 100 OF CHAR;
BEGIN
s := " A AAA";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := "AAA A ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " A AA A ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);Trim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);Trim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
s := " ";
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);RTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
Out.Char("[");Out.String(s);Out.String("]=");Out.Char(HT);LTrim(s);Out.Char("[");Out.String(s);Out.Char("]");Out.Ln;
END Trim.
Output:
[ A AAA]= [A AAA] [AAA A ]= [AAA A] [ A AA A ]= [A AA A] [ ]= [] [ ]= [] [ ]= [] []= [] []= []
Objective-C
#import <Foundation/Foundation.h>
@interface NSString (RCExt)
-(NSString *) ltrim;
-(NSString *) rtrim;
-(NSString *) trim;
@end
@implementation NSString (RCExt)
-(NSString *) ltrim
{
NSInteger i;
NSCharacterSet *cs = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for(i = 0; i < [self length]; i++)
{
if ( ![cs characterIsMember: [self characterAtIndex: i]] ) break;
}
return [self substringFromIndex: i];
}
-(NSString *) rtrim
{
NSInteger i;
NSCharacterSet *cs = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for(i = [self length] -1; i >= 0; i--)
{
if ( ![cs characterIsMember: [self characterAtIndex: i]] ) break;
}
return [self substringToIndex: (i+1)];
}
-(NSString *) trim
{
return [self
stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
@end
int main()
{
@autoreleasepool {
NSString *s = @" this is a string ";
NSLog(@"'%@'", s);
NSLog(@"'%@'", [s ltrim]);
NSLog(@"'%@'", [s rtrim]);
NSLog(@"'%@'", [s trim]);
}
return 0;
}
OCaml
let left_pos s len =
let rec aux i =
if i >= len then None
else match s.[i] with
| ' ' | '\n' | '\t' | '\r' -> aux (succ i)
| _ -> Some i
in
aux 0
let right_pos s len =
let rec aux i =
if i < 0 then None
else match s.[i] with
| ' ' | '\n' | '\t' | '\r' -> aux (pred i)
| _ -> Some i
in
aux (pred len)
let trim s =
let len = String.length s in
match left_pos s len, right_pos s len with
| Some i, Some j -> String.sub s i (j - i + 1)
| None, None -> ""
| _ -> assert false
let ltrim s =
let len = String.length s in
match left_pos s len with
| Some i -> String.sub s i (len - i)
| None -> ""
let rtrim s =
let len = String.length s in
match right_pos s len with
| Some i -> String.sub s 0 (i + 1)
| None -> ""
we put the previous code in a file called "trim.ml", and then we test these functions in the toplevel:
$ ocaml # #use "trim.ml" ;; val left_pos : string -> int -> int option = <fun> val right_pos : string -> int -> int option = <fun> val trim : string -> string = <fun> val ltrim : string -> string = <fun> val rtrim : string -> string = <fun> # let s = " \t \r \n String with spaces \t \r \n " ;; val s : string = " \t \r \n String with spaces \t \r \n " # trim s ;; - : string = "String with spaces" # ltrim s ;; - : string = "String with spaces \t \r \n " # rtrim s ;; - : string = " \t \r \n String with spaces"
Since OCaml version 4.00.0 there is a function String.trim.
OpenEdge/Progress
DEF VAR cc AS CHAR INIT " string with spaces ".
MESSAGE
"|" + LEFT-TRIM( cc ) + "|" SKIP
"|" + RIGHT-TRIM( cc ) + "|" SKIP
"|" + TRIM( cc ) + "|"
VIEW-AS ALERT-BOX.
- Output:
--------------------------- Message --------------------------- |string with spaces | | string with spaces| |string with spaces| --------------------------- OK ---------------------------
Perl
sub ltrim { shift =~ s/^\s+//r }
sub rtrim { shift =~ s/\s+$//r }
sub trim { ltrim rtrim shift }
# Usage:
my $p = " this is a string ";
print "'", $p, "'\n";
print "'", trim($p), "'\n";
print "'", ltrim($p), "'\n";
print "'", rtrim($p), "'\n";
Phix
with javascript_semantics constant s = "\t test \n" ?s ?trim_head(s) ?trim_tail(s) ?trim(s)
- Output:
"\t test \n" "test \n" "\t test" "test"
PHP
There is a built-in function that already does this.
<?php
/**
* @author Elad Yosifon
*/
$string = ' this is a string ';
echo '^'.trim($string) .'$'.PHP_EOL;
echo '^'.ltrim($string).'$'.PHP_EOL;
echo '^'.rtrim($string).'$'.PHP_EOL;
- Output:
^this is a string$ ^this is a string $ ^ this is a string$
Picat
Using the built-in functions lstrip/1
, rstrip/1
, strip/1
, and strip/2
.
import util.
go =>
S = " Jabberwocky ",
println("<" ++ S ++ ">"),
println("<" ++ lstrip(S) ++ ">"),
println("<" ++ rstrip(S) ++ ">"),
println("<" ++ strip(S) ++ ">"),
println("<" ++ strip("\t\r\bTwas brillig, and the slithy toves\r \t\v"," \b\v\t\r\v") ++ ">"),
nl.
- Output:
< Jabberwocky > <Jabberwocky > < Jabberwocky> <Jabberwocky> <Twas brillig, and the slithy toves>
PicoLisp
(de trimLeft (Str)
(pack (flip (trim (flip (chop Str))))) )
(de trimRight (Str)
(pack (trim (chop Str))) )
(de trimBoth (Str)
(pack (clip (chop Str))) )
Test:
: (trimLeft " ^G ^I trimmed left ^L ") -> "trimmed left ^L " : (trimRight " ^G ^I trimmed right ^L ") -> " ^G ^I trimmed right" : (trimBoth " ^G ^I trimmed both ^L ") -> "trimmed both"
PL/I
put ( trim(text, ' ', '') ); /* trims leading blanks. */
put ( trim(text, '', ' ') ); /* trims trailing blanks. */
put ( trim(text) ); /* trims leading and trailing */
/* blanks. */
To remove any white-space character(s) in a portable way:-
declare whitespace character(33) value
((substr(collate(), 1, 32) || ' '));
put ( trim(text, whitespace) ); /* trims leading white space. */
put ( trim(text, '', whitespace) ); /* trims trailing white space. */
put ( trim(text, whitespace, whitespace) );
/* trims leading and trailing */
/* white space. */
PowerShell
$var = " Hello World "
$var.TrimStart() # String with leading whitespace removed
$var.TrimEnd() # String with trailing whitespace removed
$var.Trim() # String with both leading and trailing whitespace removed
Output:
Hello World Hello World Hello World
Prolog
Works with SWI-Prolog.
strip :-
In = " There are unwanted blanks here! ",
strip_left(In, OutLeft),
format('In : ~s__~n', [In]),
format('Strip left : ~s__~n', [OutLeft]),
strip_right(In, OutRight),
format('Strip right : ~s__~n', [OutRight]),
strip(In, Out),
format('Strip : ~s__~n', [Out]).
strip_left(In, Out) :-
strip_action(In, Out, []).
strip_right(In, Out) :-
reverse(In, RIn),
strip_left(RIn, ROut),
reverse(ROut, Out).
strip(In, Out) :-
strip_left(In, Tmp),
strip_right(Tmp, Out).
strip_action([X|T]) -->
{\+code_type(X, graph), !},
strip_action(T).
strip_action(X) --> X.
Output :
?- strip. In : There are unwanted blanks here! __ Strip left : There are unwanted blanks here! __ Strip right : There are unwanted blanks here!__ Strip : There are unwanted blanks here!__ true.
SWI-Prolog has an integrated version of strip : normalize_space(-Out, +In)
?- In = ' There are unwanted blanks here! ', normalize_space(atom(Out), In). In = ' There are unwanted blanks here! ', Out = 'There are unwanted blanks here!'.
alternative using DCG strings
:- system:set_prolog_flag(double_quotes,chars) .
strip_leading(INPUTz,OUTPUTz)
:-
prolog:phrase(strip_leading(OUTPUTz),INPUTz)
.
strip_trailing(INPUTz,OUTPUTz)
:-
prolog:phrase(strip_trailing(OUTPUTz),INPUTz)
.
strip_leading_and_trailing(INPUTz,OUTPUTz)
:-
prolog:phrase(strip_leading_and_trailing(OUTPUTz),INPUTz)
.
strip_leading(OUTPUTz)
-->
strip_leading_spacious ,
strip_leading_nonspacious(OUTPUTz)
.
strip_trailing(OUTPUTz)
-->
strip_trailing_nonspacious(OUTPUTz) ,
strip_trailing_spacious
.
strip_leading_and_trailing(OUTPUTz)
-->
strip_leading_spacious ,
strip_trailing_nonspacious(OUTPUTz) ,
strip_trailing_spacious
.
strip_leading_spacious
-->
[CHAR] ,
{ prolog:char_type(CHAR,space) } ,
! ,
strip_leading_spacious
.
strip_leading_spacious
-->
[]
.
strip_leading_nonspacious([OUTPUT|OUTPUTz])
-->
[OUTPUT] ,
! ,
strip_leading_nonspacious(OUTPUTz)
.
strip_leading_nonspacious([])
-->
[]
.
strip_trailing_nonspacious([OUTPUT|OUTPUTz])
-->
\+ strip_trailing_spacious ,
[OUTPUT] ,
! ,
strip_trailing_nonspacious(OUTPUTz)
.
strip_trailing_nonspacious([])
-->
[]
.
strip_trailing_spacious
-->
[CHAR] ,
{ prolog:char_type(CHAR,space) } ,
! ,
strip_trailing_spacious
.
strip_trailing_spacious
-->
\+ [_] % .i.e. at end-of-string .
.
- Output:
/* ?- strip_leading(" Hello World ",OUTPUTz) . OUTPUTz = ['H', e, l, l, o, ' ', 'W', o, r, l, d, ' ']. ?- strip_trailing(" Hello World ",OUTPUTz). OUTPUTz = [' ', 'H', e, l, l, o, ' ', 'W', o, r, l, d]. ?- strip_leading_and_trailing(" Hello World ",OUTPUTz). OUTPUTz = ['H', e, l, l, o, ' ', 'W', o, r, l, d]. ?-
PureBasic
Note, if only spaces need to be removed, PureBasic provides commands that do this: LTrim(), RTrim(), and Trim(). To handle a larger selection of whitespace the following functions meet the task.
;define the whitespace as desired
#whitespace$ = " " + Chr($9) + Chr($A) + Chr($B) + Chr($C) + Chr($D) + Chr($1C) + Chr($1D) + Chr($1E) + Chr($1F)
Procedure.s myLTrim(source.s)
Protected i, *ptrChar.Character, length = Len(source)
*ptrChar = @source
For i = 1 To length
If Not FindString(#whitespace$, Chr(*ptrChar\c))
ProcedureReturn Right(source, length + 1 - i)
EndIf
*ptrChar + SizeOf(Character)
Next
EndProcedure
Procedure.s myRTrim(source.s)
Protected i, *ptrChar.Character, length = Len(source)
*ptrChar = @source + (length - 1) * SizeOf(Character)
For i = length To 1 Step - 1
If Not FindString(#whitespace$, Chr(*ptrChar\c))
ProcedureReturn Left(source, i)
EndIf
*ptrChar - SizeOf(Character)
Next
EndProcedure
Procedure.s myTrim(source.s)
ProcedureReturn myRTrim(myLTrim(source))
EndProcedure
If OpenConsole()
PrintN(#DQUOTE$ + myLTrim(" Top ") + #DQUOTE$)
PrintN(#DQUOTE$ + myRTrim(" Tail ") + #DQUOTE$)
PrintN(#DQUOTE$ + myTrim(" Both ") + #DQUOTE$)
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
- Output:
"Top " " Tail" "Both"
Python
>>> s = ' \t \r \n String with spaces \t \r \n '
>>> s
' \t \r \n String with spaces \t \r \n '
>>> s.lstrip()
'String with spaces \t \r \n '
>>> s.rstrip()
' \t \r \n String with spaces'
>>> s.strip()
'String with spaces'
>>>
Quackery
As a dialogue in the Quackery shell.
/O> [ reverse trim reverse ] is trim-trailing ( $ --> $ )
... [ trim trim-trailing ] is trim-both-ends ( $ --> $ )
... $ ' a quick quack ' echo$ say "!" cr
... $ ' a quick quack ' trim echo$ say "!" cr
... $ ' a quick quack ' trim-trailing echo$ say "!" cr
... $ ' a quick quack ' trim-both-ends echo$ say "!" cr
...
a quick quack !
a quick quack !
a quick quack!
a quick quack!
Stack empty.
R
s <- " Ars Longa "
trimws(s)
[1] "Ars Longa"
trimws(s, "left")
[1] "Ars Longa "
trimws(s, "right")
[1] " Ars Longa"
Racket
#lang racket
;; Using Racket's `string-trim'
(define str " \n\t foo bar \r\n ")
;; both sides:
(string-trim str) ; -> "foo bar"
;; one side:
(string-trim str #:right? #f) ; -> "foo bar \r\n "
(string-trim str #:left? #f) ; -> " \n\t foo bar"
;; can also normalize spaces:
(string-normalize-spaces (string-trim str)) ; -> "foo bar"
Raku
(formerly Perl 6)
my $s = "\r\n \t\x2029 Good Stuff \x202F\n";
say $s.trim;
say $s.trim.raku;
say $s.trim-leading.raku;
say $s.trim-trailing.raku;
- Output:
Good Stuff "Good Stuff" "Good Stuff \n" "\r\n \t Good Stuff"
Red
>> trim/head " remove leading white space "
== "remove leading white space "
>> trim/tail " remove trailing white space "
== " remove trailing white space"
>> trim " remove both white spaces "
== "remove both white spaces"
Retro
'__this_is_a_test___ s:trim-left
'__this_is_a_test___ s:trim-right
'__this_is_a_test___ s:trim
REXX
version 1
/*REXX program demonstrates how to strip leading and/or trailing spaces (blanks). */
yyy=" this is a string that has leading/embedded/trailing blanks, fur shure. "
say 'YYY──►'yyy"◄──" /*display the original string + fence. */
/*white space also includes tabs (VT, HT), among other characters.*/
/*all examples in each group are equivalent, only the option's 1st*/
/*character is examined. */
noL=strip(yyy,'L') /*elide any leading white space. */
noL=strip(yyy,"l") /* (the same as the above statement.) */
noL=strip(yyy,'leading') /* " " " " " " */
say 'noL──►'noL"◄──" /*display the string with a title+fence*/
noT=strip(yyy,'T') /*elide any trailing white space. */
noT=strip(yyy,"t") /* (the same as the above statement.) */
noT=strip(yyy,'trailing') /* " " " " " " */
say 'noT──►'noT"◄──" /*display the string with a title+fence*/
noB=strip(yyy) /*elide leading & trailing white space.*/
noB=strip(yyy,) /* (the same as the above statement.) */
noB=strip(yyy,'B') /* " " " " " " */
noB=strip(yyy,"b") /* " " " " " " */
noB=strip(yyy,'both') /* " " " " " " */
say 'noB──►'noB"◄──" /*display the string with a title+fence*/
/*elide leading & trailing white space,*/
noX=space(yyy) /* including white space between words.*/
say 'nox──►'noX"◄──" /*display the string with a title+fence*/
/*stick a fork in it, we're all done. */
output
YYY──► this is a string that has leading/embedded/trailing blanks, fur shure. ◄── noL──►this is a string that has leading/embedded/trailing blanks, fur shure. ◄── noT──► this is a string that has leading/embedded/trailing blanks, fur shure.◄── noB──►this is a string that has leading/embedded/trailing blanks, fur shure.◄── nox──►this is a string that has leading/embedded/trailing blanks, fur shure.◄──
version 2
/* REXX ***************************************************************
* 01.1.2012 Walter Pachl taking care of all non-printable chars
**********************************************************************/
pc='abcdefghijklmnopqrstuvwxyz'
pc=pc||translate(pc)'äöüÄÖÜß1234567890!"§&/()=?*+''#;:_,.-<>^!'
x01='01'x
s=x01||' Hi '||x01||' there! '||x01
say pc /* all printable characters */
s=x01||' Hi '||x01||' there! '||x01 /* my source string */
Say 's >'s'<' /* show it */
p1=verify(s,pc,'M') /* find first printable char */
sl=substr(s,p1) /* start with it */
Say 'sl>'sl'<'
sr=reverse(s)
p2=verify(sr,pc,'M') /* find last printable char */
sr=left(s,length(s)-p2+1) /* end with it */
Say 'sr>'sr'<'
sb=substr(s,p1,length(s)-p1-p2+1) /* remove leading & trailing */
Say 'sb>'space(sb)'!' /* whitespace */
sa=translate(s,pc,pc!!xrange('00'x,'FF'x)) /* all nonprintable chars */
/* are translated to blanks */
sa=space(sa) /* eliminate them except 1 */
Say 'sa>'sa'<'<' /* between words */
s0=space(sa,0) /* remove all whitespace */
Say 's0>'s0'<'
Output:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜß1234567890!"§&/()=?*+'#;:_,.-<>^! s >� Hi � there! �< sl>Hi � there! �< sr>� Hi � there!< sb>Hi � there!< sa>Hi there!< s0>Hithere!<
Ring
aList = " Welcome to the Ring Programming Language "
see aList + nl
see trim(aList) + nl
RPL
Recursivity provides short code.
≪ IF DUP 1 1 SUB " " == THEN 2 OVER SIZE SUB TRIM END IF DUP DUP SIZE DUP SUB " " == THEN 1 OVER SIZE 1 - SUB TRIM END ≫ ‘TRIM’ STO
" What if true luxury was space? " TRIM
- Output:
1: "What if true luxury was space?"
Ruby
s = " \t\v\r\n\ffoo bar \t\v\r\n\f"
p s
p s.lstrip # remove leading whitespaces
p s.rstrip # remove trailing whitespaces
p s.strip # remove both leading and trailing whitespace
- Output:
" \t\v\r\n\ffoo bar \t\v\r\n\f" "foo bar \t\v\r\n\f" " \t\v\r\n\ffoo bar" "foo bar"
Run BASIC
string$ = " abcdefg "
print " Top:";trim$(string$+"|") ' top left trim
print "Bottom:";trim$("|"+string$) ' bottom right trim
print " Both:";trim$(string$) ' both left and right
end
Top:abcdefg Bottom: abcdefg Both:abcdefg
Rust
fn main() {
let spaces = " \t\n\x0B\x0C\r \u{A0} \u{2000}\u{3000}";
let string_with_spaces = spaces.to_owned() + "String without spaces" + spaces;
assert_eq!(string_with_spaces.trim(), "String without spaces");
assert_eq!(string_with_spaces.trim_left(), "String without spaces".to_owned() + spaces);
assert_eq!(string_with_spaces.trim_right(), spaces.to_owned() + "String without spaces");
}
Sather
class MAIN is
ltrim(s :STR) :STR is
i ::= 0;
loop while!(i < s.size);
if " \t\f\v\n".contains(s[i]) then
i := i + 1;
else
break!;
end;
end;
return s.tail(s.size - i);
end;
rtrim(s :STR) :STR is
i ::= s.size-1;
loop while!(i >= 0);
if " \t\f\v\n".contains(s[i]) then
i := i - 1;
else
break!;
end;
end;
return s.head(i+1);
end;
trim(s :STR) :STR is
return ltrim(rtrim(s));
end;
main is
p ::= " this is a string ";
#OUT + ltrim(p).pretty + "\n";
#OUT + rtrim(p).pretty + "\n";
#OUT + trim(p).pretty + "\n";
end;
end;
Scala
def trimLeft(str: String) = str dropWhile(_.isWhitespace)
def trimRight(str: String) = str take (str.lastIndexWhere(!_.isWhitespace) + 1)
def trimRight2(str: String) = trimLeft(str reverse) reverse
def trim(str: String) = str trim
def testTrim() = {
val str = " \u001F String with spaces \t \n \r "
println("original : |" + str + "|")
println("trimLeft : |" + trimLeft(str) + "|")
println("trimRight : |" + trimRight(str) + "|")
println("trimRight2: |" + trimRight2(str) + "|")
println("trim : |" + trim(str) + "|")
}
- Output:
original : | � String with spaces | trimLeft : |String with spaces | trimRight : | � String with spaces| trimRight2: | � String with spaces| trim : |String with spaces|
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
const string: testStri is " \t \r \n String with spaces \t \r \n ";
begin
writeln(ltrim(testStri));
writeln(rtrim(testStri));
writeln(trim(testStri));
end func;
Sidef
var s = " \t\v\r\n\ffoo bar \t\v\r\n\f";
say s.strip_beg.dump; # remove leading whitespaces
say s.strip_end.dump; # remove trailing whitespaces
say s.strip.dump; # remove both leading and trailing whitespace
- Output:
"foo bar \t\13\r\n\f" " \t\13\r\n\ffoo bar" "foo bar"
Smalltalk
String extend
[
ltrim [
^self replacingRegex: '^\s+' with: ''.
]
rtrim [
^self replacingRegex: '\s+$' with: ''.
]
trim [
^self ltrim rtrim.
]
]
|a|
a := ' this is a string '.
('"%1"' % {a}) displayNl.
('"%1"' % {a ltrim}) displayNl.
('"%1"' % {a rtrim}) displayNl.
('"%1"' % {a trim}) displayNl.
SNOBOL4
s1 = s2 = " Hello, people of earth! "
s2 = CHAR(3) s2 CHAR(134)
&ALPHABET TAB(33) . prechars
&ALPHABET POS(127) RTAB(0) . postchars
stripchars = " " prechars postchars
* TRIM() removes final spaces and tabs:
OUTPUT = "Original: >" s1 "<"
OUTPUT = "With trim() >" REVERSE(TRIM(REVERSE(TRIM(s1)))) "<"
* Remove all non-printing characters:
OUTPUT = "Original: >" s2 "<"
s1 POS(0) SPAN(stripchars) =
OUTPUT = "Leading: >" s1 "<"
s2 ARB . s2 SPAN(stripchars) RPOS(0)
OUTPUT = "Trailing: >" s2 "<"
s2 POS(0) SPAN(stripchars) =
OUTPUT = "Full trim: >" s2 "<"
END
- Output:
Original: > Hello, people of earth! < With trim() >Hello, people of earth!< Original: >� Hello, people of earth! �< Leading: >Hello, people of earth! < Trailing: >� Hello, people of earth!< Full trim: >Hello, people of earth!<
Standard ML
local
open Substring
in
val trimLeft = string o dropl Char.isSpace o full
and trimRight = string o dropr Char.isSpace o full
and trim = string o dropl Char.isSpace o dropr Char.isSpace o full
end
Stata
See strtrim in Stata help. Use the equivalent ustrtrim functions with Unicode strings.
s = " ars longa "
"("+strtrim(s)+")"
(ars longa)
"("+strltrim(s)+")"
(ars longa )
"("+strrtrim(s)+")"
( ars longa)
"("+stritrim(s)+")"
( ars longa )
Tcl
Whitespace stripping is done with string trim
and related commands:
set str " hello world "
puts "original: >$str<"
puts "trimmed head: >[string trimleft $str]<"
puts "trimmed tail: >[string trimright $str]<"
puts "trimmed both: >[string trim $str]<"
- Output:
original: > hello world < trimmed head: >hello world < trimmed tail: > hello world< trimmed both: >hello world<
TI-83 BASIC
PROGRAM:WHITESPC
Input Str1
0→M
Menu(" REMOVE ","TRAILIN WHTSPC",A,"LEADING WHTSPC",C,"BOTH",B)
Lbl B
1→M
Lbl A
While sub(Str1,length(Str1)-1),1)=" "
sub(Str1,1,length(Str1)-1)→Str1
End
If M=1
Then
Goto C
Else
Goto F
End
Lbl C
While sub(str1,1,1)=" "
sub(Str1,2,length(Str1)-1)→Str1
End
Lbl F
Disp "'"+Str1+"'"
TorqueScript
Remove leading whitespace
$string = " yep "; $string = LTrim($string); echo($string);
Remove trailing whitespace
$string = " yep "; $string = RTrim($string); echo($string);
Remove leading and trailing white spaces
$string = " yep "; $string = trim($string); echo($string);
Output
"yep " " yep" "yep"
True BASIC
LET s$ = " \tRosetta Code \v\f\r\n"
PRINT ltrim$(s$) ! remove leading whitespace
PRINT rtrim$(s$) ! remove trailing whitespace
PRINT trim$(s$) ! remove both leading and trailing whitespace
END
TUSCRIPT
$$ MODE TUSCRIPT
str= " sentence w/whitespace before and after "
trimmedtop=EXTRACT (str,":<|<> :"|,0)
trimmedtail=EXTRACT (str,0,":<> >|:")
trimmedboth=SQUEEZE(str)
PRINT "string <|", str," >|"
PRINT "trimmed on top <|",trimmedtop,">|"
PRINT "trimmed on tail <|", trimmedtail,">|"
PRINT "trimmed on both <|", trimmedboth,">|"
- Output:
string <| sentence w/whitespace before and after >| trimmed on top <|sentence w/whitespace before and after >| trimmed on tail <| sentence w/whitespace before and after>| trimmed on both <|sentence w/whitespace before and after>|
TXR
Pattern Matching Language Exercise
Here, no builtin functions are used, just text pattern matching logic. Two functions are written, conforming to the proper filter convention, and then employed as filters.
@(define trim_left (in out))
@ (next :list in)
@/[ \t]*/@out
@(end)
@(define trim_right (in out))
@ (local blanks middle)
@ (next :list in)
@ (cases)
@ {blanks /[ \t]*/}@middle@/[\t ]+/
@ (bind out `@blanks@middle`)
@ (or)
@ out
@ (end)
@(end)
@line_of_input
@(output)
trim-left: [@{line_of_input :filter (:fun trim_left)}]
trim_right: [@{line_of_input :filter (:fun trim_right)}]
trim_both: [@{line_of_input :filter ((:fun trim_left) (:fun trim_right))}]
@(end)
- Output:
$ echo "" | txr trim.txr - trim-left: [] trim_right: [] trim_both: [] $ echo "a" | txr trim.txr - trim-left: [a] trim_right: [a] trim_both: [a] $ echo " a" | txr trim.txr - trim-left: [a] trim_right: [ a] trim_both: [a] $ echo " a " | txr trim.txr - trim-left: [a ] trim_right: [ a] trim_both: [a] $ echo " a b " | txr trim.txr - trim-left: [a b ] trim_right: [ a b] trim_both: [a b]
Using Lisp Primitives
Trimming whitespace from both ends is a builtin:
$ txr -p '(trim-str " a b ")'
"a b"
An unnecessarily cryptic, though educational, left trim:
$ txr -p '[(do progn (del [@1 0..(match-regex @1 #/\s*/)]) @1) " a b "]'
"a b "
Explanation: the basic structure is [function " a b "]
where the function is an anonymous lambda generated using the do
operator. The function is applied to the string " a b "
.
The structure of the do
is (do progn (blah @1) @1)
where the forms make references to implicit argument @1
, and so the generated lambda has one argument, essentially being: (lambda (arg) (blah arg) arg)
: do something with the argument (the string) and then return it.
What is done with the argument is this: (del [@1 0..(match-regex @1 #/\s+/)])
. The match-regex
function returns the number of characters at the front of the string which match the regex \s*
: one or more spaces. The return value of this is used to express a range 0..length
which is applied to the string. The syntax (del [str from..to])
deletes a range of characters in the string.
Lastly, a pedestrian right trim:
(defun trim-right (str)
(for ()
((and (> (length str) 0) (chr-isspace [str -1])) str)
((del [str -1]))))
(format t "{~a}\n" (trim-right " a a "))
(format t "{~a}\n" (trim-right " "))
(format t "{~a}\n" (trim-right "a "))
(format t "{~a}\n" (trim-right ""))
Output:
{ a a} {} {a} {}
Ursala
#import std
white = ==` !| not @iNC %sI
trim_left = white-~r
trim_right = white~-l
trim_both = trim_left+ trim_right
#cast %sgUL
main = <.trim_left,trim_right,trim_both> ' string with spaces '
- The
white
predicate tests an argument for whiteness by either comparing it to a literal space character or testing whether the singleton list containing it is of a string (%s
) type. - The
-~
postfix operator takes a predicate to a function that takes a string to a pair of strings whose concatenation is the original string and whose left side is the maximal prefix of the original string whose members satisfy the predicate. - The
r
suffix on the-~
predicate extracts the right side of the pair of strings in the result. - The
~-
operator is similar to the-~
operator except that is concerned with the maximal suffix whose members satisfy the predicate. - The
l
suffix extracts the right side.
- Output:
< 'string with spaces ', ' string with spaces', 'string with spaces'>
Vala
Strip Leading White Space
string s = " word ";
string s_chug = s.chug();
Strip Trailing White Space
string s = " word ";
string s_chomp = s.chomp();
Strip Leading & Trailing White Space
string s = " word ";
string s_strip = s.strip();
VBA
Public Sub test()
'LTrim trims leading spaces
'RTrim trims tailing spaces
'Trim trims both leading and tailing spaces
s = " trim "
Debug.Print """" & s & """"
Debug.Print """" & LTrim(s) & """"
Debug.Print """" & RTrim(s) & """"
Debug.Print """" & WorksheetFunction.trim(s) & """"
'these functions do not remove tabs or newlines
End Sub
- Output:
" trim " "trim " " trim" "trim"
VBScript
Function LeftTrim(s)
Set regex = New RegExp
With regex
.Pattern = "^\s*"
If .Test(s) Then
LeftTrim = .Replace(s,"")
Else
LeftTrim = s
End If
End With
End Function
Function RightTrim(s)
Set regex = New RegExp
With regex
.Pattern = "\s*$"
If .Test(s) Then
RightTrim = .Replace(s,"")
Else
RightTrim = s
End If
End With
End Function
'testing the functions
WScript.StdOut.WriteLine LeftTrim(" RosettaCode")
WScript.StdOut.WriteLine RightTrim("RosettaCode ")
WScript.StdOut.WriteLine LeftTrim(RightTrim(" RosettaCode "))
- Output:
RosettaCode RosettaCode RosettaCode
Wren
In Wren 'whitespace' is defined as: space, tab, carriage return, and line feed characters. To trim off other non-printing characters such as form feed and vertical tab, you need to do a little more work.
var a = " \t\r\nString with leading whitespace removed"
var b = "String with trailing whitespace removed \t\r\n"
var c = " \t\r\nString with both leading and trailing whitespace removed \t\r\n"
var d = " \t\r\n\f\vString with leading whitespace, form feed and verical tab characters removed"
System.print("'%(a.trimStart())'")
System.print("'%(b.trimEnd())'")
System.print("'%(c.trim())'")
System.print("'%(d.trimStart(" \t\r\n\f\v"))'") // similar overloads of trimEnd and trim exist
- Output:
'String with leading whitespace removed' 'String with trailing whitespace removed' 'String with both leading and trailing whitespace removed' 'String with leading whitespace, form feed and vertical tab characters removed'
XBasic
PROGRAM "progname"
VERSION "0.0000"
DECLARE FUNCTION Entry ()
FUNCTION Entry ()
s$ = " \tRosetta Code \v\f\r\n"
PRINT LTRIM$(s$) ' remove leading whitespace
PRINT RTRIM$(s$) ' remove trailing whitespace
PRINT TRIM$(s$) ' remove both leading and trailing whitespace
END FUNCTION
END PROGRAM
XPL0
code ChOut=8, CrLf=9, Text=12;
string 0; \use zero-terminated string convention
func StripLead(S0); \Strip leading whitespace (<=$20) from string
char S0;
char S1(80); \BEWARE: very temporary string space returned
int I, J, C, Stripped;
[I:= 0; J:= 0; Stripped:= false;
repeat C:= S0(I); I:= I+1;
if Stripped ! C>$20 ! C=0 then
[S1(J):= C; J:= J+1; Stripped:= true];
until C=0;
return S1;
];
func StripTrail(S0); \Strip trailing whitespace (<=$20) from string
char S0;
char S1(80); \BEWARE: very temporary string space returned
int I, SI;
[I:= 0;
loop [if S0(I) <= $20 then
[SI:= I;
while S0(I) <= $20 do
[if S0(I) = 0 then [S1(SI):= 0; return S1];
S1(I):= S0(I); I:= I+1;
];
]
else [S1(I):= S0(I); I:= I+1];
];
];
char String;
[String:= " Hello World ";
Text(0, StripLead(String)); ChOut(0, ^!); CrLf(0);
Text(0, StripTrail(String)); ChOut(0, ^!); CrLf(0);
Text(0, StripTrail(StripLead(String))); ChOut(0, ^!); CrLf(0);
]
Output:
Hello World ! Hello World! Hello World!
Yabasic
s$ = "\t test \n"
print "--",ltrim$(s$),"--"
print "--",rtrim$(s$),"--"
print "--",trim$(s$),"--"
zkl
Remove white space from both end of string:
"\t\n hoho\n\t\ ".strip() //-->"hoho"
fcn removeLeadingWS(s){ n:=0;
try{ while(s[n].isSpace()){ n+=1 } }catch{""}fallthrough{ s[n,*] }
}
removeLeadingWS("\t\n hoho\n\t\ ") //-->"hoho\n\t "
removeLeadingWS("") //-->""
fcn removeTrailingWS(s){ n:=-1;
try{ while(s[n].isSpace()){ n-=1 } s[0,n+1] }catch{""}
}
removeTrailingWS("\t\n hoho\n\t\ ") //-->"\t\n hoho"
removeTrailingWS("\t\n \n\t\ ") //-->""
Zoea
program: trim_left
input: ' abcd'
output: 'abcd'
program: trim_right
input: 'abcd '
output: 'abcd'
program: trim
input: ' abcd '
output: 'abcd'
Zoea Visual
- Programming Tasks
- String manipulation
- 11l
- Action!
- Ada
- ALGOL 68
- Amazing Hopper
- AppleScript
- Arturo
- AutoHotkey
- AWK
- BaCon
- BASIC
- BASIC256
- BBC BASIC
- BQN
- Bracmat
- Burlesque
- C
- Gadget
- C sharp
- C++
- Clojure
- COBOL
- Common Lisp
- Crystal
- D
- Delphi
- Pascal
- DWScript
- EasyLang
- EchoLisp
- Elena
- Elixir
- Emacs Lisp
- Erlang
- Euphoria
- F Sharp
- Factor
- Forth
- Fortran
- FreeBASIC
- FutureBasic
- Gambas
- Go
- Groovy
- Haskell
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Jsish
- Julia
- K
- Kotlin
- Ksh
- Lambdatalk
- Lasso
- Liberty BASIC
- Logtalk
- Lua
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Mercury
- Nemerle
- NetRexx
- NewLISP
- Nim
- Oberon-2
- Objective-C
- OCaml
- OpenEdge/Progress
- Perl
- Phix
- Phix/basics
- PHP
- Picat
- PicoLisp
- PL/I
- PowerShell
- Prolog
- PureBasic
- Python
- Quackery
- R
- Racket
- Raku
- Red
- Retro
- REXX
- Ring
- RPL
- Ruby
- Run BASIC
- Rust
- Sather
- Scala
- Seed7
- Sidef
- Smalltalk
- SNOBOL4
- Standard ML
- Stata
- Tcl
- TI-83 BASIC
- TorqueScript
- True BASIC
- TUSCRIPT
- TXR
- Ursala
- Vala
- VBA
- VBScript
- Wren
- XBasic
- XPL0
- Yabasic
- Zkl
- Zoea
- Zoea Visual