Jump to content

Photo

Comparing the first n characters of strings

papyrus programming strings skyrim scripting script help

  • Please log in to reply
2 replies to this topic

#1
JosephRussell

JosephRussell

    Resident poster

  • Premium Member
  • 3,106 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! :)



#2
Ghaunadaur

Ghaunadaur

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,190 posts
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, 30 May 2018 - 10:59 AM.


#3
JosephRussell

JosephRussell

    Resident poster

  • Premium Member
  • 3,106 posts

Substring is just what I need! That's marvellous, thank you :D







Also tagged with one or more of these keywords: papyrus, programming, strings, skyrim, scripting, script, help

Page loaded in: 1.078 seconds