WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: pguild on February 09, 2023, 10:23:09 AM

Title: STRTRIM(Clip) does not trim as well as my barber
Post by: pguild on February 09, 2023, 10:23:09 AM
When I copy text from a website with Ctrl-A Ctrl-C, and then use strtrim with code like this . . .

Clip = strtrim(Clip)

. .  and then put the variable Clip into a multiline editbox in a dialog,
there is always extra space below and above the text that shows.

Then I need to manually delete the space. I'm seeking to delete the
spaces above and below the string that I am putting into the multiline edit box.
Any ideas?   8)

Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: JTaylor on February 09, 2023, 11:17:01 AM
Is it possible you have @CR and or @LF characters rather than spaces?   

I made a feature request sometime back to add that as one of the flags to StrTrim().  Not sure if it made the list of changes or not.

Jim
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: td on February 09, 2023, 01:47:50 PM
Quote from: pguild on February 09, 2023, 10:23:09 AM
When I copy text from a website with Ctrl-A Ctrl-C, and then use strtrim with code like this . . .

Clip = strtrim(Clip)

. .  and then put the variable Clip into a multiline editbox in a dialog,
there is always extra space below and above the text that shows.

Then I need to manually delete the space. I'm seeking to delete the
spaces above and below the string that I am putting into the multiline edit box.
Any ideas?   8)

As the help file states, "... function removes spaces and tab characters from the beginning and end of a text string." Other characters require a little more but not much more work. For example, you can use StrClean to remove carriage returns and line feed characters. Something like this

Code (winbatch) Select
stuff = "  ":@lf:"dummy stand-in":@crlf
cleaned = StrTrim(StrClean(stuff, @crlf, "", 0, 1))


Of course, you can modify the above to fit your exact needs.
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: JTaylor on February 09, 2023, 02:23:41 PM
Having an option for @LF & @CR in StrTrim() would be helpful for when you want leading and/or trailing characters gone but not those within the text.   Not an uncommon situation when pulling data from web pages.

Jim
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: td on February 09, 2023, 03:18:26 PM
A little more involved.

Code (winbatch) Select
; Bugs included at no extra charge.
#definefunction TrimStr(_target, _chars)
   a = ArrayFromStr(_target)
   nMax = ArrInfo(a, 1)-1
   for i=0 to nMax
      if !StrIndex(_chars, a[i], 1, @Fwdscan) then break
   next
   for j=nMax to 0 by -1
      if !StrIndex(_chars, a[j], 1, @Fwdscan) then break
   next
   return StrSub(_target,i+1, (j-i)+1);
#endfunction

stuff = "  ":@lf:"dummy":@lf:"stand-in":@crlf 
cleaned = TrimStr(stuff, " ":@tab:@crlf)


Didn't want anyones fingers to get too tired.
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: JTaylor on February 09, 2023, 03:49:40 PM
Obviously anyone can write code to solve pretty much any issue.  That is true for most of the functions in WinBatch.   I even added a version of StrTrim() in my extender for dealing with this very thing.   Just nice when one person does it so 1,000 others don't have to :)

Jim
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: td on February 09, 2023, 05:12:42 PM
The post had nothing to do with any statement about adding more functionality to the WIL StrTrim function. It was about helping the OP if he had the problem you mentioned. Anyone who doesn't mind performing a copy-and-paste can use the one posted above to avoid writing their own.
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: pguild on February 09, 2023, 09:42:07 PM
Thanks for the TrimStr function. It worked perfectly!  :D
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: ChuckC on February 10, 2023, 06:12:28 AM
Another option...

Use WIL's CLR hosting support and consume .NET's string.Trim() method.  One of its overloads accepts an array of characters that specify what's considered to be whitespace to be trimmed from the leading/trailing portions of the string.
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: td on February 10, 2023, 07:59:42 AM
Unfortunately, the "System.String" class does not play well with WIL CLR hosting.
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: ChuckC on February 10, 2023, 10:38:38 AM
Oh, yeah, Doh!  I forgot that there's a conversion to a variant type instead of preserving the string.

I did some digging around and adapted some other WinBatch + CLR code to make an in-memory assembly from the dynamic compilation of a little bit of C# code.  It's suitable for packaging in some kind of UDF library where the compilation happens one time at script startup and then the end result can be re-used as often as needed in the script.


ObjectClrOption('useany', 'System')

objCSharp = ObjectClrNew('Microsoft.CSharp.CSharpCodeProvider')

objParams = ObjectClrNew('System.CodeDom.Compiler.CompilerParameters')

objParams.ReferencedAssemblies.Add( "System.dll" )

objParams.ReferencedAssemblies.Add( "System.Runtime.dll" )

objParams.GenerateInMemory = ObjectType( "VT_BOOL", 1 ) ; TRUE

objParams.GenerateExecutable = ObjectType( "VT_BOOL", 0 ) ; FALSE

objParams.CompilerOptions = "/optimize"

sCode = ""

sCode = sCode : "using System;" : @LF

sCode = sCode : "namespace StringTest" : @LF

sCode = sCode : "{" : @LF

sCode = sCode : " public class StringUtil" : @LF

sCode = sCode : " {" : @LF

sCode = sCode : " public string Trim(string StringToTrim, string WhiteSpaceChars)" : @LF

sCode = sCode : " {" : @LF

sCode = sCode : " var ws = WhiteSpaceChars.ToCharArray();" : @LF

sCode = sCode : " return StringToTrim.Trim(ws);" : @LF

sCode = sCode : " }" : @LF

sCode = sCode : " }" : @LF

sCode = sCode : "}" : @LF

objResult = objCSharp.CompileAssemblyFromSource(objParams, sCode)

;; Check for compiler errors.
If objResult.Errors.Count > 0
    ForEach ce In objResult.Errors
      Pause("Error", ce.ToString())
   Next
   Exit
EndIf


sOriginal = ChrStringToUnicode(@TAB : " " : @CRLF : " " : "Sample string with characters to be trimmed.   \ : ; " : @CRLF : " " : @TAB)

sWS = ChrStringToUnicode(" \:;" : @TAB : @CRLF)

objStringUtil = ObjectClrNew('StringTest.StringUtil')

sTrimmed = objStringUtil.Trim(sOriginal, sWS)
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: td on February 10, 2023, 01:15:50 PM
A compiled in-memory assembly is often a good approach when all else fails. 
Title: Re: STRTRIM(Clip) does not trim as well as my barber
Post by: stanl on February 11, 2023, 02:48:54 AM
TrimStr() is nice. In some older scripts I used worksheetfunction.Clean(text) but not always perfect results.