FindAndReplace (New Custom Functions)

Started by Sonmez, Jan 11, 2021, 04:31 PM

Previous topic - Next topic

Sonmez

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;}
}
if (!perfect) createAgain();

Altay

Mustafa Kemal ATATURK, the man who changed the course of history. https://en.wikipedia.org/wiki/Mustafa_Kemal_Atat%C3%BCrk

DizzasTeR

#2
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;
}

Sonmez

#3
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.
if (!perfect) createAgain();