KB-93342: Removing spaces from strings.

Question:

When editing scripts in Elipse E3, can I remove spaces from strings? And as for spaces in the middle of strings, can they be removed too? Which function(s) should I use for this purpose?

Solution:

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. When you want to remove spaces from strings in your script, there are two possibilities for fixing this, depending on where the spaces are in the string: either at its end, or in the middle of it.

To remove blanks at the string’s end (leading and/or trailing), use Trim function (as seen below):

MyVar = Trim("   vbscript  ")
'MyVar contains "vbscript"

Likewise, you can also use LTrim and RTrim functions, depending on where the blanks are.

According to Microsoft’s documentation, the LTrim, RTrim and Trim functions

“return a Variant (String) containing a copy of a specified string without leading spaces (LTrim), trailing spaces (RTrim), or both leading and trailing spaces (Trim).”

 

On the other hand, to remove spaces in the middle or next to the string, use Replace function (as seen below):

aux = "Elipse Software "
MyString = Replace(aux, " ","")
'MyString contains "ElipseSoftware"

According to Microsoft’s documentation, Replace function

“returns a String in which a specified substring has been replaced with another substring a specified number of times.”

 

Using both these functions, or a combination of them (according to the case), you will be able to remove spaces from your strings safely.

For further information on this and other functions applied to Elipse E3’s scripts, check out our section Elipse E3/Scripts.

For further information on string functions and how to use them, check out Microsoft’s documentation.

Este artigo foi útil? Was this post helpful?
Yes0
No0

Leave a Reply

Your email address will not be published.Required fields are marked *