Werbung: SecurityConsole.de verwaltet Ihre Computer mit Security Essentails aus der Cloud!
30 Tage kostenfrei testen und 20% Rabatt für Ihre Bestellung mit Promocode: WBF2685582
(Promocode gültig bis 31.12.2011)

Group:  English: Windows Server » microsoft.public.windows.server.scripting
Thread: TRIM function Limit

HTVi
TV Discussion Newsgroups

TRIM function Limit
Stefano 6/4/2007 8:58:01 AM
I'm using the TRIM function to remove leading and trailing spaces from a
string read from a file.
The problem is that I found the file may contains also trailing characters
different from spaces ( ie Tab character ) which the TRIM function does not
remove .

How can I effectively trim out all such characters from the string read ?
Thanks

RE: TRIM function Limit
J Ford 6/4/2007 1:26:00 PM
Try using the replace function on the other chars you are encountering.

strNewString = replace(trim(strOrigString), vbTab, "")

something like that maybe

"Stefano" wrote:

[Quoted Text]
> I'm using the TRIM function to remove leading and trailing spaces from a
> string read from a file.
> The problem is that I found the file may contains also trailing characters
> different from spaces ( ie Tab character ) which the TRIM function does not
> remove .
>
> How can I effectively trim out all such characters from the string read ?
> Thanks
>
Re: TRIM function Limit
"Richard Mueller [MVP]" <rlmueller-nospam[ at ]ameritech.nospam.net> 6/4/2007 6:45:09 PM
Stefano wrote:

[Quoted Text]
> I'm using the TRIM function to remove leading and trailing spaces from a
> string read from a file.
> The problem is that I found the file may contains also trailing characters
> different from spaces ( ie Tab character ) which the TRIM function does
> not
> remove .
>
> How can I effectively trim out all such characters from the string read ?
> Thanks
>

If you only want to replace leading and trailing characters, and not
embedded characters (like vbTab), I think you need to extract the first and
last characters, check if they are "special", and if they are remove and
repeat. For example:
===========
Option Explicit
Dim arrSpecial, strInput, strOutput

' Specify special charactes to be removed
' if they are leading or trailing.
' vbCrLf is actually two characters.
arrSpecial = Array(" ", vbTab, vbCr, vbLf, "_")

' Test example string.
strInput = " " & vbTab & " " & vbTab & " " & vbCrLf _
& "_" & "_ Test " & vbTab & " _ "

strOutput = LeftTrim(strInput)
strOutput = RightTrim(strOutput)

' Output with leading and trailing dots
' so we can tell if there are leading or trailing spaces.
Wscript.Echo "Output: ." & strOutput & "."

Function LeftTrim(strValue)
' Trim leading special characters.
Dim blnTrim, strSpecial, strChar

blnTrim = False
If (Len(strValue) = 0) Then
LeftTrim = ""
Exit Function
End If
strChar = Left(strValue, 1)

For Each strSpecial In arrSpecial
If (strChar = strSpecial) Then
If (Len(strValue) = 1) Then
LeftTrim = ""
Exit Function
End If
strValue = Mid(strValue, 2)
blnTrim = True
End If
Next
LeftTrim = strValue
If (blnTrim = True) Then
LeftTrim = LeftTrim(strValue)
End If
End Function

Function RightTrim(strValue)
' Trim trailing special characters.
Dim blnTrim, strSpecial, strChar

blnTrim = False
If (Len(strValue) = 0) Then
RightTrim = ""
Exit Function
End If
strChar = Right(strValue, 1)

For Each strSpecial In arrSpecial
If (strChar = strSpecial) Then
If (Len(strValue) = 1) Then
RightTrim = ""
Exit Function
End If
strValue = Right(strValue, Len(strValue) - 1)
blnTrim = True
End If
Next
RightTrim = strValue
If (blnTrim = True) Then
RightTrim = RightTrim(strValue)
End If
End Function

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net
Suche nach Orten, Städten, Postleitzahlen, Vorwahlen, Kfz-Kennzeichen