Old school Swatch Watches
/ /

XtScript Function: trim, ltrim, and rtrim

Remove whitespace (or other characters) from the beginning of a string, from the end of a string, or both.

trim in XtScript is a function that will remove extra whitespaces or a serial characters sets found from the beginning and the end of a string.

The other variation is ltrim function that will remove whitespaces or a serial of characters sets from the beginning of a string and return the result.

Otherwise, if you want the process to remove whitespaces or a serial of character sets start from the end of the string, you can use rtrim function instead.

XtScript trim, ltrim, and rtrim basic syntax:

<!--parser:xtscript-->
	# Trim whitespaces or sets of characters from the beginning and the end of a string
	call trim $val=...; $charlist=...

	# Trim whitespaces or sets of characters from the beginning of a string
	call ltrim $val=...; $charlist=...

	# Trim whitespaces or sets of characters from the end of a string
	call rtrim $val=...; $charlist=...
<!--/parser:xtscript-->

Additional argument on XtScript trim, ltrim, and rtrim function:

  • $charlist

    Example arguments and values:
    $charlist=xya, will trim "xya" found in the string.
    $charlist=A..E, will trim a range of characters started with A to E.

Learn XtScript ltrim and rtrim function from examples

Using XtScript trim function to trim a range of characters and extra whitespaces from the beginning and the end of a string.

- Code:
<div class="test-script" style="font-family:monospace; word-spacing:5px">
<!--parser:xtscript-->
	var $string = Do you know xtgem? of course you do
  	var $trimmed = call trim $val=$string; $charlist=A..o
  	var $trimmed_2 = call trim $val=$trimmed
  
  	print String: |_|<b>$string</b>|_| <br/>
  	print Trimmed: |_|<b>$trimmed</b>|_| <br/>
  	print No extra space: |_|<b>$trimmed_2</b>|_|
<!--/parser:xtscript-->
</div>
- Result:
String: |_|Do you know xtgem? of course you do|_|
Trimmed: |_| you know xtgem? of course you |_|
No extra space: |_|you know xtgem? of course you|_|

Using XtScript ltrim function to trim a range of characters and extra whitespaces from the beginning of a string.

- Code:
<div id="test-script" style="font-family:monospace; word-spacing:5px">
<!--parser:xtscript-->
	var $string = LETS learn XtScript together!	
	var $trimmed = call ltrim $val=$string; $charlist=E..T
	var $noextraspace = call ltrim $val=$trimmed

	print String: |_|<b>$string</b>|_| <br/>
	print Trimmed: |_|<b>$trimmed</b>|_| <br/>
	print No extra space: |_|<b>$noextraspace</b>|_|
<!--/parser:xtscript-->
</div>
- Result:
String: |_|LETS learn XtScript together!|_|
Trimmed: |_| learn XtScript together!|_|
No extra space: |_|learn XtScript together!|_|

Using XtScript rtrim function to trim a range of characters and extra whitespaces from the end of a string.

- Code:
<div class="test-script" style="font-family:monospace; word-spacing:5px">
<!--parser:xtscript-->
  	# Assigning a space to a variable so we can use space as the last character in the string
  	var $spa = call chr $val=32	
  
	var $string = XtScript is server side scripting for xtgem users$spa
  	var $noextraspace = call rtrim $val=$string
  	var $trimmed = call rtrim $val=$noextraspace; $charlist=e..u
  
  	print String: |_|<b>$string</b>|_| <br/>
  	print No extra space: |_|<b>$noextraspace</b>|_| <br/>
  	print Trimmed: |_|<b>$trimmed</b>|_|
<!--/parser:xtscript-->
</div>
- Result:
String: |_|XtScript is server side scripting for xtgem users |_|
No extra space: |_|XtScript is server side scripting for xtgem users|_|
Trimmed: |_|XtScript is server side scripting for xtgem |_|