Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: Sonmez on Jan 11, 2021, 04:31 PM

Title: FindAndReplace (New Custom Functions)
Post by: Sonmez on Jan 11, 2021, 04:31 PM
Source Code
function find(str,string){
  if(string.find(str)!= null){return true;}else{return false;}
}
function FindAndReplace(string,findString,Replace){
  if (find(findString,string)&&findString!=string){ local n=-1;
    do{n++
      if(n+findString.len()<=string.len()){
        if(string.slice(n,n+findString.len())==findString){ local prev=null,next=null;
          if(n==0){prev="";}else{prev=string.slice(0,n)}
          if(n+Replace.len()==string.len()){next=""}else{next=string.slice(n+findString.len(),string.len())}
          string = format("%s%s%s"prev,Replace,next); n=string.len()+1;
          return string;
        }
      }
    }while (n<string.len())
  }else{return string;}
}
Title: Re: FindAndReplace (New Custom Functions)
Post by: Altay on Jan 11, 2021, 06:46 PM
That's the way it's done.
Title: Re: FindAndReplace (New Custom Functions)
Post by: DizzasTeR on Jan 12, 2021, 04:12 AM
Quote from: |VC-TR| Yankee on Jan 11, 2021, 06:46 PMThat's the way it's done.

function onPlayerChat(player, text)
{
    if(text.find("YourWord") != null)
        text = StringGSub(text, "YourWord", "ReplacedWord");
}
function StringGSub(str, findString, replace)
{
    local index;
    do {
        index = str.find(findString, index != null ? index+findString.len() : 0);
        if (index != null)
        {
            local prev = str.slice(0, index);
            local next = str.slice(index+findString.len(), str.len());
            str = format("%s%s%s", prev, replace, next);
        }
    } while (index != null);
    return str;
}
Title: Re: FindAndReplace (New Custom Functions)
Post by: Sonmez on Jan 12, 2021, 09:13 AM
Quote from: DizzasTeR on Jan 12, 2021, 04:12 AM
Quote from: |VC-TR| Yankee on Jan 11, 2021, 06:46 PMThat's the way it's done.

function onPlayerChat(player, text)
{
    if(text.find("YourWord") != null)
        text = StringGSub(text, "YourWord", "ReplacedWord");
}
function StringGSub(str, findString, replace)
{
    local index;
    do {
        index = str.find(findString, index != null ? index+findString.len() : 0);
        if (index != null)
        {
            local prev = str.slice(0, index);
            local next = str.slice(index+findString.len(), str.len());
            str = format("%s%s%s", prev, replace, next);
        }
    } while (index != null);
    return str;
}


This is a good alternative but i think my code is easier to understand and appeal to everyone.