Author Topic: How to filter and replace a chat segment  (Read 1564 times)

Example, if I said Hello World on chat, world will be changed to Changed.

AppleScript-style psuedocode for whoever needs to know
Code: [Select]
get chatMsg
if "World" is present in chatMsg then
modify chatMsg
change "World" to "Changed"
end modify
end if
say chatMsg

I'm curious as to the most efficient way to do it. I used to use strReplace, but I noticed that's case sensitive. I've made workarounds, but I won't suggest any.

Code: [Select]
%string = "Hello world, testing.";
if(%a = strpos(%string,"hello world") > 0)
{
%string = erase(%string,%a,11);
}

If erase was a function in torque like it is in C++ that would work to remove an instance of "hello world" from the string.
But I don't know if it is.

I used to use strReplace, but I noticed that's case sensitive.
Just wondering, but won't striReplace be non-case sensitive?

Code: [Select]
%string = "Hello world, testing.";
if(%a = strpos(%string,"hello world") > 0)
{
%string = erase(%string,%a,11);
}

If erase was a function in torque like it is in C++ that would work to remove an instance of "hello world" from the string.
But I don't know if it is.

I don't want it removed, I just want it changed to another word.

Just wondering, but won't striReplace be non-case sensitive?

No, it's case sensitive. If it isn't, I have many problems with replacing "stuff" with "icecream" in the sentence "I stuffed myself" a few years ago when people cared for censors.

I don't believe striReplace exists. Only strReplace exists last time I checked.

try something like this:

%search = "World";
%change = "Changed";

%pos = strpos(%chatMsg, %search);
%newMessage = getSubstr(%chatMsg, 0, %pos) @ %change @ getSubstr(%chatMsg, %pos + strlen(%search), strlen(%chatMsg) );

try something like this:

%search = "World";
%change = "Changed";

%pos = strpos(%chatMsg, %search);
%newMessage = getSubstr(%chatMsg, 0, %pos) @ %change @ getSubstr(%chatMsg, %pos + strlen(%search), strlen(%chatMsg) );
Of course it needs to actually check if it needs to be replaced.
And it needs to keep looping until there isn't, or else it'll just replace the first occurrence.

Of course it needs to actually check if it needs to be replaced.
And it needs to keep looping until there isn't, or else it'll just replace the first occurrence.

A problem is changing for case... same word, different case... Someone post the most efficient way to do a non-case-sensitive replace or else.

I don't believe striReplace exists. Only strReplace exists last time I checked.

+

try something like this:

%search = "World";
%change = "Changed";

%pos = strpos(%chatMsg, %search);
%newMessage = getSubstr(%chatMsg, 0, %pos) @ %change @ getSubstr(%chatMsg, %pos + strlen(%search), strlen(%chatMsg) );

=

Code: [Select]
function striReplace(%source,%search,%replace)
{
if(%search !$= "")
{
%len = strLen(%search);

while((%pos = striPos(%source,%search)) != -1)
%source = getSubStr(%source,0,%pos) @ %replace @ getSubStr(%source,%pos + %len,strLen(%source));
}

return %source;
}

-snip-

Wait, what if you want to use it to change the case on words? Like, make "CAt" "Cat" ? Wouldn't that cause a doom loop? Ya, it'd complicate the code, but might be required...
« Last Edit: November 29, 2010, 12:29:33 PM by MegaScientifical »

Wait, what if you want to use it to change the case on words? Like, make "CAt" "Cat" ? Wouldn't that cause a doom loop? Ya, it'd complicate the code, but might be required...

Ok, fine. Complication for case changing go:

Code: [Select]
function striReplace(%source,%search,%replace)
{
if(%search $= "")
return %source;

%lena = strLen(%source);
%lenb = strLen(%search);

for(%i = 0; %i < %lena; %i++)
{
%part = getSubStr(%source,%i,%lenb);

if(%part !$= %search)
{
%clean = %clean @ getSubStr(%source,%i,1);
continue;
}

%clean = %clean @ %replace;
%i    += %lenb - 1;
}

return %clean;
}

Ok, fine. Complication for case changing go:

Code: [Select]
function striReplace(%source,%search,%replace)
{
if(%search $= "")
return %source;

%lena = strLen(%source);
%lenb = strLen(%search);

for(%i = 0; %i < %lena; %i++)
{
%part = getSubStr(%source,%i,%lenb);

if(%part !$= %search)
{
%clean = %clean @ getSubStr(%source,%i,1);
continue;
}

%clean = %clean @ %replace;
%i    += %lenb - 1;
}

return %clean;
}

You're some kind of God... I'm having a hard time reading that... %clean isn't setting right off... the.... I'm just going to try it out. But thanks for working with replaces involving changing a case of something.

stripos. Case insensitive strpos. I just checked it in the console, and it exists.

Code: [Select]
function striReplace(%source, %search, %replace)
{
    %len = strlen(%search);
    while((%i = stripos(%source, %search)) >= 0)
    {
        %source = getSubStr(%source, 0, %i) @ %replace @ getSubStr(%source, %i + %len, strlen(%source) - %i - %len);
    }
    return %source;
}