[Custom Teascript Syntax] Replace part of strings

General discussion about Super Mario Bros. X.
erwill
Koopa
Koopa
Posts: 15
Joined: Tue Feb 08, 2022 4:39 pm

[Custom Teascript Syntax] Replace part of strings

Postby erwill » Fri Jun 24, 2022 4:07 pm

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: show
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: show
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 “General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest