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)
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
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
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.
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
A little more involved.
; 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.
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
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.
Thanks for the TrimStr function. It worked perfectly! :D
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.
Unfortunately, the "System.String" class does not play well with WIL CLR hosting.
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)
A compiled in-memory assembly is often a good approach when all else fails.
TrimStr() is nice. In some older scripts I used worksheetfunction.Clean(text) but not always perfect results.