Code-Beispiel
vbReplace(disphelper, Runscript)
Lizenz: | Erster Autor: | Letzte Bearbeitung: |
k. A. | ytwinky | 22.04.2012 |
Englishspeaking visitors please see below..
Bei den vielen Replace-Funktionen fehlt natürlich noch eine, die von Millonen Benutzern schon benutzt wird: Es ist die Replace()-Funktion von VBScript.
Diese lässt sich mit RunScript() aus dem \examples-Verzeichnis aber aufrufen!
Nachteil: Reguläre Ausdrücke lassen sich nur in jscript verwenden..
(und ich kann weder jscript(richtig) noch reguläre Ausdrücke..)
siehe Code
There are a lot of replace-functions on this portal, but one is missing, which is used by millions of users: It is the Replace()-function of vbcript.
We can use it via RUNScript() from the \examples-folder
Disadvantage: regular expressions may only be used using jscript..
(and I don't know jscript(exactly) nor regular expressions)
regards
ytwinky
#define UNICODE
#include "vbCompat.bi"
#include "disphelper/disphelper.bi"
Sub RunScript ( byval szRetIdentifier as LPWSTR, _
byval pResult as LPVOID, _
byval szScript as LPWSTR, _
byval szLanguage as LPWSTR )
DISPATCH_OBJ(scrCtl)
if( SUCCEEDED( dhCreateObject( "MSScriptControl.ScriptControl", NULL, @scrCtl ) ) ) then
if( SUCCEEDED( dhPutValue( scrCtl, ".Language = %T", szLanguage ) ) ) Then '%T is a string..
dhPutValue(scrCtl, ".AllowUI = %b", TRUE) '%b is boolean, obviously..
dhPutValue(scrCtl, ".UseSafeSubset = %b", FALSE)
if( pResult = FALSE ) then
dhCallMethod( scrCtl, ".Eval(%T)", szScript )
else
dhGetValue( szRetIdentifier, pResult, scrCtl, ".Eval(%T)", szScript )
end if
end if
end if
SAFE_RELEASE( scrCtl )
end sub
Function vbReplace(Src As String, _ 'sourcestring
Search As String, _ 'searchstring
ReplaceWith As String, _ 'replacestring
Start As Integer=1, _ 'startposition of search
Count As Integer=-1, _ 'number of occurences, -1 means all
Mode As Integer=1) As String '0=binary, 1=text search, 1=ignore cases
Var s=Space(2048)'beware: there must be space enough to store the NEW string, 2048 is only a guess..
dhInitialize( TRUE ) 'seems to boolean, should it initialize something if set to FALSE?
dhToggleExceptions( TRUE ) 'there are to possibilities, TRUE and FALSE, TRUE works..
RunScript("%s", @s, !"Replace(\34" &Src &!"\34,\34" &Search &!"\34,\34" &ReplaceWith &!"\34," &Start &"," &Count &"," &Mode &")", "VBScript")
' ^--this means string, %d means double but would not make any sense here ;-))
dhUninitialize(TRUE) 'no comment^^
Function=Left(s , InStr(s, !"\0"))
s=""
End Function
Dim As String dest, src="This is an especially long textstring? No, it is my simple example..", expr="is", repl="at"
dest=vbReplace(src, expr, repl, 1, 1, 0) 'change here to see the result eg: 1, -1, 0 ..
Print " src=" &Src &!"\ndest=" &dest &!"\nEniki..";
GetKey
Zusätzliche Informationen und Funktionen |
- Das Code-Beispiel wurde am 22.04.2012 von ytwinky angelegt.
- Die aktuellste Version wurde am 22.04.2012 von ytwinky gespeichert.
|
|