Does a Function to replace one word in a string with another exist?

I searched in DOORS help, and did not find a direct function to allow me to replace a word in a string with another.

I can replace a character just fine, but not a whole word.

Is anyone aware of such a function?

Thank you.
Gedinfo - Wed Sep 26 14:35:53 EDT 2012

Re: Does a Function to replace one word in a string with another exist?
Mathias Mamsch - Fri Sep 28 14:04:48 EDT 2012

There is none in DXL (hard to believe for everyone having programmed another programming language), but there are several in this forum. Look for example here: https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14539676&#14539676

Or just use the search function and enter 'replace'. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Does a Function to replace one word in a string with another exist?
Mathias Mamsch - Fri Sep 28 14:12:00 EDT 2012

Mathias Mamsch - Fri Sep 28 14:04:48 EDT 2012
There is none in DXL (hard to believe for everyone having programmed another programming language), but there are several in this forum. Look for example here: https://www.ibm.com/developerworks/forums/thread.jspa?messageID=14539676&#14539676

Or just use the search function and enter 'replace'. Regards, Mathias


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Just realized that I posted a wrong link and that I do not immediately find an implementation of a string replace function. Well here is one:
 

string replace (string sSource, string sSearch, string sReplace) {
    int iLen = length sSource
    if (iLen == 0) return ""
    
    int iLenSearch = length(sSearch)
    
    if (iLenSearch == 0) { 
        //raiseError ("Parameter error", "in strings.inc/replace: search string must not be empty")
        return "" 
    }
    
    // read the first char for latter comparison -> speed optimization
    char firstChar = sSearch[0]
    
    Buffer s = create() 
    int pos = 0, d1,d2;    
    int i
    
    while (pos < iLen) { 
        char ch = sSource[pos]; 
        bool found = true
        
        if (ch != firstChar) {pos ++; s+= ch; continue}
        for (i = 1; i < iLenSearch; i++) {
           if (sSource[pos+i] != sSearch[i]) { found = false; break }
        }
        if (!found) {pos++; s+= ch; continue}
        s += sReplace
        pos += iLenSearch
    }
    
    string result = stringOf s
    delete s
    return result
}

 


Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Does a Function to replace one word in a string with another exist?
Gedinfo - Mon Oct 01 09:49:25 EDT 2012

Thank you

Re: Does a Function to replace one word in a string with another exist?
PatrickGuay - Mon Oct 01 10:09:47 EDT 2012

As an alternative

I think it is a good idea to seperate functions into library files (include file .inc). Create two files, one to store string functions and another for buffer functions. Store them in a convient place in your file system where they can easily be called from your script.

In the buffer library declare the following function:
 

int buffertools_replace(Buffer &subject, Buffer find_str, Buffer replace) {
    int l_find_str = length(find_str)
        int l_subject = length(subject)
        int i = 0
        Buffer tmp_buf = create()
        int last_index = 0, current_index
        int instances = 0
        
        while((current_index = buffertools_find(subject, find_str, last_index)) != -1) {
                combine(tmp_buf, subject, last_index, current_index - 1)
                tmp_buf += replace
                instances++
                last_index = current_index + l_find_str
        }
 
        combine(tmp_buf, subject, last_index)
        
        delete(subject)
        subject = tmp_buf
 
        return instances
}

 


In the string library declare the following function:

 

 

int stringtools_replace(string &subject, string find_str, string replace) {
    Buffer subject_buffer  = create(length(subject))
        Buffer find_str_buffer = create(length(find_str))
        Buffer replace_buffer  = create(length(replace))
        
        subject_buffer  = subject
        find_str_buffer = find_str
        replace_buffer  = replace
        
        int res = buffertools_replace(subject_buffer, find_str_buffer, replace_buffer)
        
        subject = stringOf(subject_buffer)
        
        delete(subject_buffer)
        delete(find_str_buffer)
        delete(replace_buffer)
        
        return res
}



Cheers,
Patrick



 

Re: Does a Function to replace one word in a string with another exist?
llandale - Mon Oct 01 14:08:59 EDT 2012

Just because I'm in a prickly mood, I'd like to point out that if your Search string is a sub-set of your Replace string you will end up with unpleasant results. I was tasked with replacing all occurances of "WZY" with "vWXYz" in the specs, and ended up turning:
  • WXY shall..
into
  • vvvvvWXYzzzzz shall..
After running the script a few times.

I did actually resolve it with "ReplaceStringSafe()", it was REAL ugly, and now I cannot find that function. I think it was:
  • is there a unsafe sub-set problem? (does Replace contain Search?)
  • find the offset in Replace where Search starts
  • When I find Search in the main string; extract the part of the main that would contain Replace and see if it does. e.g. if "WXY" starts in position 5, then extract string [4:8] and see if it is "vWXYz".
  • if so, do NOT replace it.
  • Seems to me I gave up when I realized the above algorithm failed trying to replace safely "abab" with "ababab".

-Louie

Re: Does a Function to replace one word in a string with another exist?
PatrickGuay - Tue Oct 02 08:11:45 EDT 2012

Here is the missing search function in my snippet
 

int buffertools_find(Buffer &subject, Buffer find_str, int starting) {
    
        int l_find_str = length(find_str)
        int l_subject = length(subject)
        Buffer sub_b
        
        int i
        int i_max = starting + l_subject - l_find_str
 
        for(i = starting ; i <= i_max ; i++) {
                if(subject[i] == find_str[0]) {
                        sub_b = subject[i: i + l_find_str - 1]
                        if(sub_b == find_str) {
                                delete(sub_b)
                                return i
                        }
                        delete(sub_b)
                }
        }
        return -1
}
 
 
int buffertools_find(Buffer &subject, string find_str, int starting) {
        Buffer find_str_buffer = create()
        
        find_str_buffer = find_str
        
        int res = buffertools_find(subject, find_str_buffer, starting)
        
        delete(find_str_buffer)
        
        return res
}

Re: Does a Function to replace one word in a string with another exist?
janwitte - Fri Feb 16 06:09:38 EST 2018

Mathias Mamsch - Fri Sep 28 14:12:00 EDT 2012

Just realized that I posted a wrong link and that I do not immediately find an implementation of a string replace function. Well here is one:
 

string replace (string sSource, string sSearch, string sReplace) {
    int iLen = length sSource
    if (iLen == 0) return ""
    
    int iLenSearch = length(sSearch)
    
    if (iLenSearch == 0) { 
        //raiseError ("Parameter error", "in strings.inc/replace: search string must not be empty")
        return "" 
    }
    
    // read the first char for latter comparison -> speed optimization
    char firstChar = sSearch[0]
    
    Buffer s = create() 
    int pos = 0, d1,d2;    
    int i
    
    while (pos < iLen) { 
        char ch = sSource[pos]; 
        bool found = true
        
        if (ch != firstChar) {pos ++; s+= ch; continue}
        for (i = 1; i < iLenSearch; i++) {
           if (sSource[pos+i] != sSearch[i]) { found = false; break }
        }
        if (!found) {pos++; s+= ch; continue}
        s += sReplace
        pos += iLenSearch
    }
    
    string result = stringOf s
    delete s
    return result
}

 


Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Thank You

Re: Does a Function to replace one word in a string with another exist?
EHcnck - Fri Feb 16 08:17:40 EST 2018

janwitte - Fri Feb 16 06:09:38 EST 2018

Thank You

string vbReplace(string inStr, string FindStr, string RepStr, bool global, bool IgnoreCase) {
        //https://msdn.microsoft.com/en-us/library/ms974570.aspx
        //https://www.regular-expressions.info/vbscript.html
        OleAutoObj RegExpObj = oleCreateAutoObject("VBScript.RegExp")
        if (null(RegExpObj)) {
                //error ("Cannot connect to OLE Application >VBScript.RegExp<!")
                return inStr
        }

        string Str = null

        OleAutoArgs args = create()
        clear(args)

        olePut(RegExpObj, "Global", global)
        olePut(RegExpObj, "Pattern", FindStr)
        olePut(RegExpObj, "IgnoreCase", IgnoreCase)
        put(args, inStr)
        put(args, RepStr)
        oleMethod(RegExpObj, "Replace", args, Str)

        clear(args)
        delete(args)
    oleCloseAutoObject(RegExpObj)
        RegExpObj = null

        return Str
}

string vbReplace(string inStr, string FindStr, string RepStr, bool IgnoreCase) {
        return vbReplace(inStr, FindStr, RepStr, true, IgnoreCase)
}

string vbReplace(string inStr, string FindStr, string RepStr) {
        return vbReplace(inStr, FindStr, RepStr, true, true)
}