General EQWatcher Concepts > Venturing into the Unknown > String Manipulation
List of commands
Note that "integer" is not a valid variable type, but is used in this documentation to mean any integer type (signed long, unsigned long, char, byte). Be forewarned that a negative length or string position creates a paradox in the space-time continuum! Okay... well don't use negative integers.
atoi(integer n1)
itoa(string data)
left(string data, integer length)
LowerCase(string data)
mid(string data, integer position, integer length)
right(string data, integer length)
strcat(string destination, string s1, string s2, . . . string sn)
strchr(string destination, char data, integer position)
strcmp(string string1, string string2)
strlen(string data)
strpos(string data, integer n1)
substr(string data, string substring, integer position)
atoi(integer n1) Command
Retrieves a string value from an integer
Return value
A string containing the number from n1
Parameters
n1
The source integer
Example
itoa(string data) Command
Retrieves an integer value from a string
Return value
An integer type value retrieved from the string data
Parameters
data
The source string
Example
left(string data, integer length) Command
Extracts the left part of a string
Return value
A string containing the left length integer of characters of the string data.
Parameters
data
The string with data to be retrieved
length
The integer of characters to extract.
Example
// This example speaks the left 9 characters of a string, which turns out to be "EQWatcher".
function main()
{
string example;
example=left("EQWatcher example function",9);
SpeakSync(example);
}
LowerCase(string data) Command
Returns an entirely lower-case copy of a string
Return value
A string containing an entirely lower-case copy of the string data.
Parameters
data
The source string
Example
mid(string data, integer position, integer length) Command
Extracts a middle part of a string
Return value
A string containing length integer of characters of the string data, starting with the character at position. The first character in a string is at position zero (0).
Parameters
data
The string with data to be retrieved
position
Position to start at in the string. String positions are zero-based, so the first character is found at 0. A shortened version of mid(string,0,length) is left(string,length).
length
The integer of characters to extract
Example
// This example speaks a string of 7 characters from the middle of a string, which turns out to be "example"
function main()
{
string example;
example=mid("EQWatcher example function",10,7);
SpeakSync(example);
}
right(string data, integer length) Command
Extracts the right part of a string
Return value
A string containing the right length integer of characters of the string data.
Parameters
data
The string with data to be retrieved
length
The integer of characters to extract.
Example
// This example speaks the right 8 characters of a string, which turns out to be "function".
function main()
{
string example;
example=right("EQWatcher example function",8);
SpeakSync(example);
}
strcat(string destination, string s1, string s2, . . . string sn) Command
Concatenates strings to the end of the destination string
Return value
none
Parameters
destination
The destination string, which logically becomes destination=destination+s1+s2+s3+s4+ . . . sn
s1, s2, . . . sn
Any integer of strings to concatenate
Example
// This alias adds a SND_WAVE trigger, automatically ignoring the time stamp
public string TriggerName;
public string TriggerText;
public string TriggerSound;
alias("trigger wav \"@TriggerName@\" \"@TriggerText@\" \"@TriggerSound@\"")
{
string text;
strcat(text,"[@trash@] ",TriggerName);
if (AddTrigger(TriggerName,text,SND_WAVE,TriggerSound))
{
SpeakSync("Trigger added");
}
else
{
SpeakSync("Trigger not added");
}
}
strchr(string destination, char data, integer position) Command
Replaces a specific character position with a new character
Return value
none
Parameters
destination
The destination string
data
The character to store
position
The zero-based position in destination to store data
Example
// This example turns "bill" into "ball"
function main()
{
string example;
example="bill";
strchr(example,'a',1);
SpeakSync(example);
}
strcmp(string string1, string string2) command
Compares two strings
Return value
signed long values:
-1 if string1 is less than string2
0 if string1 is equal to string2
+1 if string1 is greater than string2
Parameters
string1
First string to compare
string2
Second string to compare
Example
// This trigger is from the combat module of the core script, compares MobName to LastMob
trigger("[@trash@] %combatant% pierce@s@ @MobName@ for @Damage@ point@s@ of
damage.")
{
signed long mobequal;
LastHit=clock();
mobequal=strcmp(LastMob,MobName);
if(mobequal!=0)
{ // new mob
NewMob(MobName);
}
TotalPierceHits++;
FightTotal+=Damage;
TotalDamage+=Damage;
TotalPierceDamage+=Damage;
FightPiercing+=Damage;
}
strlen(string data) Command
Retrieves the length of a string
Return value
A integer indicating the length of the string data
Parameters
data
The string to be measured
Example
// This example speaks the length of a string, which should be 26
function main()
{
SpeakSync(strlen("EQWatcher example function"));
}
strpos(string data, integer position) Command
Extracts a single character from a string
Return value
A numeric value representing the character at zero-based position in data
Parameters
data
The string with data to be retrieved
position
The position of the character to extract
Example
// This example takes part of one string using strpos, and replaces part of
another string with that.
// The final value of str2 is "dread"
function main()
{
string str1;
string str2;
char value;
str1="Fred"
str2="dream"
value=strpos(str1,3);
strchr(str2,value,4);
SpeakSync(str2);
}
substr(string data, string substring, integer position) Command
Searches for a substring in a string, case sensitive
Return value
A numeric value determining the starting character position of substring in string. If the substring is not found in the string, the value -1 is returned.
Parameters
data
The string to be searched
substring
The substring to search for in data. This search is case sensitive.
position
Position to start at in the string. String positions are zero-based, so the first character is found at 0.
Example
// This example searches for an arbitrary substring in a string, then speaks the word starting at that position
constant str1 "EQWatcher example function";
function main()
{
signed long pos;
signed long endpos;
pos=substr(str1,"example",0); // Search for "example" starting at the
beginning.. =10
if (pos>=0) // if the position is negative, the word was not found. we
want only non-negative.
{
string example;
endpos=substr(str1," ",pos); // Find the first space from
position pos.. =17
if (endpos==-1) // in case the word is the last word in the
string
{
endpos=strlen(str1); // this will not be reached
in this example
}
example=mid(str1,pos,endpos-pos); // endpos-pos gives a
length instead of position value.. =7
SpeakSync(example); // "example" will be the spoken word in
this example.
}
}