Warcraft III resources & community, 2003–2006 · archived

StringReplace(), StringSplit(), StringComparison()

not rated
Submitted by N-a-z-g-u-lText Parsing0 downloads
StringReplace() replaces a string in another string, example:
StringReplace("abc", "a", "x", true) = "xbc"

StringSplit() returns parts of the string
StringSplit("try this!", " ", 0, true) = "try"
StringSplit("try this!", " ", 1, true) = "this!"

StringComparison() compares two strings
StringComparison("a", "a", true) = true
StringComparison("a", "A", true) = false
StringComparison("a", "A", false) = true

Source

function StringComparison takes string String1, string String2, boolean CaseSensitive returns boolean
	local string Compare1 = String1
	local string Compare2 = String2
	if not CaseSensitive then
		set Compare1 = StringCase(Compare1, false)
		set Compare2 = StringCase(Compare2, false)
	endif
	return Compare1 == Compare2
endfunction

function StringSplit takes string Base, string Split, integer Index, boolean CaseSensitive returns string
	local string SearchIn = Base + Split
	local string SearchFor = Split
	local integer i = 0
	local integer SplitLength = StringLength( Split )
	local integer EndLength = StringLength( Base )
	local integer FoundIndex = 0
	local integer LastSplit = -1
	if SplitLength == 0 then
		return ""
	endif
	if not CaseSensitive then
		set SearchIn = StringCase(SearchIn, false)
		set SearchFor = StringCase(SearchFor, false)
	endif
	loop
		exitwhen i > EndLength
		if SubString( SearchIn, i, i + SplitLength ) == SearchFor then
			if FoundIndex == Index then
				return "'" + SubString( Base, LastSplit + SplitLength, i ) +"'"
			endif
			set FoundIndex = FoundIndex + 1
			set LastSplit = i
			set i = i + SplitLength
		else
			set i = i + 1
		endif
	endloop
	return ""
endfunction

function StringReplace takes string Base, string RemovePart, string NewPart, boolean CaseSensitive returns string
	local string SearchIn = Base
	local string SearchFor = RemovePart
	local integer i = 0
	local integer RemovePartLength = StringLength( RemovePart )
	local integer NewPartLength = StringLength( NewPart )
	local integer EndLength = StringLength( Base ) - 1
	local string NewString = ""
	if RemovePartLength == 0 then
		return Base
	endif
	if not CaseSensitive then
		set SearchIn = StringCase(SearchIn, false)
		set SearchFor = StringCase(SearchFor, false)
	endif
	loop
		exitwhen i > EndLength
		if SubString( SearchIn, i, i + RemovePartLength ) == SearchFor then
			set NewString = NewString + NewPart
			set i = i + RemovePartLength
		else
			set NewString = NewString + SubString( Base, i, i + 1 )
			set i = i + 1
		endif
	endloop
	return NewString
endfunction

Comments

0

No comments.