- Joined
- Nov 25, 2008
- Messages
- 1,309
This function
Primary use of this function for me is parsing chat commands (ie: "-give gold 100 Zeatherann").
Example Use:
Edit: Will craft this into a string library, well, minus the vJASS library/endlibrary words which yall can add if ya like.
GetWord
allows you to split a string up by whitespace separated groups of characters. It starts looking for words from the starting position S.Primary use of this function for me is parsing chat commands (ie: "-give gold 100 Zeatherann").
JASS:
// Returns the end of the word starting at S. It ignores leading whitespace. Whitespace characters are spaces, tabs, and newlines.
// To make use of the return value, use SubString(String,Index,GetWord(String,Index)).
// Notes: You'll receive the leading spaces ignored by this function.
function GetWord takes string str,integer S returns integer
local integer Len=StringLength(str)
local boolean Beg=false
local boolean Spc
local string C=""
loop
set C=SubString(str,S,S+1)
set Spc=(C==" ")or(C=="\t")or(C=="\n")
if Beg and Spc then
return S
elseif Spc==false then
set Beg=true
endif
set S=S+1
if S>=Len then
return Len
endif
endloop
endfunction
Example Use:
JASS:
function Chat takes nothing returns nothing
// ***** Local variables *****
local string Txt=GetEventPlayerChatString()
local integer Len=StringLength(Txt)
local integer Pos=GetWord(Txt,0)
local string Str=SubString(Txt,0,Pos)
local integer Old
call DisplayTextToPlayer(Player(0),0,0,"\""+Str+"\"")
loop
exitwhen Pos==Len
set Old=Pos+1
set Pos=GetWord(Txt,Pos)
set Str=SubString(Txt,Old,Pos)
call DisplayTextToPlayer(Player(0),0,0,"\""+Str+"\"")
endloop
endfunction
function InitTrig_Chat takes nothing returns nothing
set gg_trg_Chat=CreateTrigger()
call TriggerRegisterPlayerChatEvent(gg_trg_Chat,Player(0),"",false)
call TriggerAddCondition(gg_trg_Chat,Filter(function Chat))
endfunction
JASS:
// String.Find
// string Str: The source string to find the position of the wanted string.
// string Txt: The string you want to find within the source string.
// integer S: The starting position you want to look for the string.
//
// returns integer: Returns the position of the found text, or -1 if no string is found.
//
// Notes: This function can and will create a ton of unique strings for the string table, which makes it eat up more RAM.
function StringFind takes string Str,string Txt,integer S returns integer
local integer Len=StringLength(Str)
local integer L2=StringLength(Txt)
loop
if SubString(Str,S,S+L2)==Txt then
return S
endif
set S=S+1
if S+L2>Len then
return -1
endif
endloop
endfunction
Last edited: