I searched in DOORS help, and did not find a direct function to allow me to replace a word in a string with another. |
Re: Does a Function to replace one word in a string with another exist? 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:04:48 EDT 2012 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
}
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? |
Re: Does a Function to replace one word in a string with another exist?
As an alternative
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
}
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
}
|
Re: Does a Function to replace one word in a string with another exist?
I did actually resolve it with "ReplaceStringSafe()", it was REAL ugly, and now I cannot find that function. I think it was:
-Louie |
Re: Does a Function to replace one word in a string with another exist?
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? 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
}
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? 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)
}
|