Author Topic: STRTRIM(Clip) does not trim as well as my barber  (Read 944 times)

pguild

  • Jr. Member
  • **
  • Posts: 88
  • The dog is always right!
    • MasteryLearning
STRTRIM(Clip) does not trim as well as my barber
« 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)


JTaylor

  • Pundit
  • *****
  • Posts: 1948
    • Data & Stuff Inc.
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #1 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

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #2 on: February 09, 2023, 01:47:50 pm »
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
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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

  • Pundit
  • *****
  • Posts: 1948
    • Data & Stuff Inc.
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #3 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

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #4 on: February 09, 2023, 03:18:26 pm »
A little more involved.

Code: Winbatch
 ; 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

JTaylor

  • Pundit
  • *****
  • Posts: 1948
    • Data & Stuff Inc.
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #5 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

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #6 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

pguild

  • Jr. Member
  • **
  • Posts: 88
  • The dog is always right!
    • MasteryLearning
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #7 on: February 09, 2023, 09:42:07 pm »
Thanks for the TrimStr function. It worked perfectly!  :D

ChuckC

  • Sr. Member
  • ****
  • Posts: 435
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #8 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.

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #9 on: February 10, 2023, 07:59:42 am »
Unfortunately, the "System.String" class does not play well with WIL CLR hosting.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

ChuckC

  • Sr. Member
  • ****
  • Posts: 435
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #10 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.

Code: [Select]
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)

td

  • Tech Support
  • *****
  • Posts: 4440
    • WinBatch
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #11 on: February 10, 2023, 01:15:50 pm »
A compiled in-memory assembly is often a good approach when all else fails. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

  • Pundit
  • *****
  • Posts: 1849
Re: STRTRIM(Clip) does not trim as well as my barber
« Reply #12 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.