Charmed Quark Systems, Ltd. - Support Forums and Community

Full Version: variable (Local & Global) replace function question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am playing around with the media repo at the moment and I am in a position where I want to manipulate a local variable.

Currently I need to replace a single character with three characters (space with %20), but the replace command will only replace a single character with a single character.

Is there a way I can change a substring with a different length substring.

You'll have to do a search, a cut, and an insert probably.
so you mean, split the local variable into multiple local variable based on a desired character as the split point, add a new string at each split point and then append them again?

Can the replace command be modified to allow multiple characters and not just one character?

I was thinking, find the space, which gives you an index. Do a cut of one character at the index. Then do an insert of %20 at that index.
OK I understand that

How do I loop for a unknown number of spaces. There could be none, there could be five or six?
That you can't do at the action level. I think what you really need is an 'encode URL' that will take a URL and expand it out to the full legal URL form, right? There is support for that at the CML level, but not at the action level yet.
That was just as example

I am trialing sage as well as the http commands for xbmc.

both want different formats so I need to do small manipulations on the string in questions.

Can the replace command for a variable be expanded so that it can replace a single character with multiple characters?

It could be, or another one provided that does. But it'll be a bit before I can get to it.
Here is a method I wrote for the SNMP driver that I think will do what you want. You just need to put it into a macro's start method.

//-------------- ReplaceSubStr -------------------------------------------
// Replaces the substring strSought with strReplace in strSearched.  Not
// case sensitive, and will do multiple replacements.
Method ReplaceSubStr ([InOut] String strSearched,
                    [In] String strSought,
                    [In] String strReplace) Returns Boolean
        String strWork;
        String strBefore;
        String strAfter;
        Card4  c4Pos;
        Card4  c4Len;
        Card4  c4WorkLen;
        Card4  c4AfterPos;
        Boolean bRetVal;

    strWork := strSearched;
    c4Len := strSought.GetLength();
    bRetVal := False;
    While (strWork.FindSubStr(strSought, c4Pos, False))
        // Init Variables
        bRetVal := True;
        strBefore := "";
        strAfter := "";

        // Get Before String
        If (c4Pos > 0)
            strWork.ExtractSubStr(0, c4Pos, strBefore);

        // Get After String
        c4AfterPos := c4Pos+c4Len;
        c4WorkLen := strWork.GetLength();
        If (c4AfterPos < c4WorkLen)
            strWork.ExtractSubStr(c4AfterPos, Card4.kMaxValue, strAfter);

        // Do Replacement
        strWork := strBefore + strReplace + strAfter;
    strSearched := strWork;
    Return bRetVal;