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)