Jump to content

Comparing the first n characters of strings


Recommended Posts

Hi everyone,

 

I've got two strings, eg. "Apple 1" and "Apple 2" and I want to compare the first 5 characters only, so I can test for the word "Apple" and have it return true for both strings. Any ideas if/how that can be done?

 

Any help would be greatly appreciated! :)

Link to comment
Share on other sites

If you don't mind using SKSE, you could do something like this

 

bool Function FunctionX(string String1, string String2)
	int i = 0
	bool j
	While i < 5
		if StringUtil.GetNthChar(String1, i) == StringUtil.GetNthChar(String2, i)
			j = true
		else
			j = false
			return j
		endif
		i += 1
	EndWhile
	return j
EndFunction

 

edit...or better use SubString

bool Function FunctionX(string String1, string String2)
		if StringUtil.SubString(String1, 0, 5) == StringUtil.SubString(String2, 0, 5)
			return true
		else
			return false
		endif
EndFunction
Edited by Ghaunadaur
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...