Strip whitespace from a string/Top and tail: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(RPL: add section)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 4 users not shown)
Line 221:
+ 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}}==
Line 597 ⟶ 653:
return 0;
}</syntaxhighlight>
 
==={{header|Gadget}}===
 
<p>Gadget C-library:
[https://github.com/DanielStuardo/Gadget Gadget C-library in Github]
</p>
<syntaxhighlight lang="c">
#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
</syntaxhighlight>
{{out}}
<pre>
Right Trim = [
ESTE MENSAJE SERÁ MODIFICADO]
Left Trim = [REVERSADO ÁRES EJASNEM ETSE
]
All Trim = [ESTE MENSAJE SERÁ MODIFICADO]
 
</pre>
 
=={{header|C sharp|C#}}==
Line 753 ⟶ 842:
PrintLn('"' + TrimRight(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}}==
Line 1,152 ⟶ 1,273:
 
=={{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.
 
Line 1,397 ⟶ 1,558:
julia> strip(s)
"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}}==
Line 3,031 ⟶ 3,207:
=={{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="ecmascriptwren">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"
9,476

edits