[Custom Syntax] Replace part of strings

Description: You made a script? Submit it here to show your creation to the public, share, take reviews, provision of the help and support is here!
Moderators: Lx Xzit, Yoshi021, 5438A38A, Semi-moderatos, Moderators

Erwill M
Topic author, Count
Count
Avatar
Erwill M
Topic author, Count
Count
Age: 24
Reputation: 189
Posts: 295
Joined: 29 Nov 2015

Post #1by Erwill » 24 Jun 2022, 22:50

I created two custom Syntaxes for string values that replace or remove part of the value.

replace(source, text, start, size)
Returns the text with the replaced part

source: The text source which will have the replaced part
text: The replaced part
start: the specified start where the part will be replaced (First character is 1)
size: the size of the part which will be replaced (inferior to 0 means the size will take the size of the replaced part)

Examples
Basic characters replacement

Code: Select all

str(a) = "Super Mario Bros X" 'Replace "Mario" with "Luigi"
str(a) = replace(str(a), "Luigi", 7, -1) 'Return "Super Luigi Bros X"


Characters replacement with different length

Code: Select all

str(a) = "Super Mario Bros X" 'Replace "Bros" with "Creator Ultra"
str(a) = replace(str(a), "Creator Ultra", 13, 4) 'Return "Super Mario Creator Ultra X"


Characters insertion

Code: Select all

str(a) = "Super Mario Bros X" 'Add "38A version" before "X"
str(a) = replace(str(a), "38A version ", 18, 0) 'Return "Super Mario Bros 38A version X"


Script

Code: Select all

script replace(source as string, text as string, start as double, size as double, return string)
   start -= 1
   if size < 0 then size = len(text)
   if start < 0 or start+size > len(source)
      call debug("____REPLACE STRING ERROR____")
      call debug("Out of bound place on string")
      call debug("Start-end position: "&(start+1)&"|"&start+size)
      call debug("Length text: "&len(source))
      call debug("_____________________________")
   else
      return left(source, start)+text+right(source, len(source)-start-size)
   end
end script


remove(source, start, size)
Returns the text with the removed part

source: The text source which will have the removed part
start: the specified start where the part will be removed (First character is 1)
size: the size of the part which will be removed

Examples
Removed part

Code: Select all

str(a) = "Super Mario Bros X" 'Remove "Bros"
str(a) = remove(str(a), 13, 5) 'Return "Super Mario X"


Script

Code: Select all

script remove(source as string, start as double, size as double, return string)
   start -= 1
   if size < 0 then size = 0
   if start < 0 then start = 0
   if start+size > len(source)
      return left(source, start)
   else
      return left(source, start)+right(source, len(source)-start-size)
   end
end script

Return to “Script submission”

Who is online (over the past 5 minutes)

Users browsing this forum: 1 guest