here is some code from DAQuest that chops up longer strings and delivers them during a certain time period.
what i don't understnd is what a substring is. could somebody give a quick overview of what this code does? (comments next to lines would be good).
float fDuration = SL_GetReadSpeed( sString );
sString = sString + " . ";
int i;
int i2;
string sPrint;
string sSentence = "";
int iStringLoc = FindSubString( sString, " ", 0 );
int iStringLocOld = 0;
string sTemp;
int iTemp = GetStringLength (sString );
int iTemp2;
for (i = 1; i < 500; i++)
{
sPrint = SubString( sString, 0, iStringLoc );
sString = StringRight(sString, (iTemp-iStringLoc)-1 );
iTemp = GetStringLength (sString );
iStringLoc = FindSubString( sString, " ", 0 );
iTemp2 = GetStringLength ( sSentence );
if ( (iTemp2 > 37) || (iStringLoc == -1) )
{
if (iStringLoc == -1) i = 500;
if ((sSentence != "") && (sSentence != " ") && (sSentence != " ") )
DisplayFloatyMessage(oWhere, sSentence, FLOATY_MESSAGE, 16777215, fDuration);
sSentence = "";
}
sSentence = sSentence + sPrint + " ";
what is a substring?
Débuté par
gordonbrown82
, juin 12 2010 05:18
#1
Posté 12 juin 2010 - 05:18
#2
Posté 12 juin 2010 - 05:34
Basically it's the part of a string.
For instance
SubString( sString, 0, iStringLoc );
will return the iStringLoc character long fragment of sString starting at index 0. (which is the first char)
And in this code, FindSubString() is used to determine where to cut the strings since it returns the starting index of the substring in the string :
FindSubString( sString, " ", 0 );
Will look for the index on the first space (" ") substring in sString and will return that index.
In the end :
string sString = "Three-headed monkey";
PrintToLog(SubString(sString, FindSubString(sString, " ", 0), 6));
Would look for the index / position of the space in the string and print the substring "monkey" to the log.
For instance
SubString( sString, 0, iStringLoc );
will return the iStringLoc character long fragment of sString starting at index 0. (which is the first char)
And in this code, FindSubString() is used to determine where to cut the strings since it returns the starting index of the substring in the string :
FindSubString( sString, " ", 0 );
Will look for the index on the first space (" ") substring in sString and will return that index.
In the end :
string sString = "Three-headed monkey";
PrintToLog(SubString(sString, FindSubString(sString, " ", 0), 6));
Would look for the index / position of the space in the string and print the substring "monkey" to the log.
Modifié par Phaenan, 12 juin 2010 - 05:34 .
#3
Posté 12 juin 2010 - 05:39
but doesn't that mean that if i have a string like "let's eat some candy and some popcorn"
it would reurn eat because that's after the first space? wouldn't everything get shopped up into too small pieces?
it would reurn eat because that's after the first space? wouldn't everything get shopped up into too small pieces?
#4
Posté 12 juin 2010 - 06:05
It only displays once the concatenated string is longer than 37 characters or there is nothing left to concatenate.
#5
Posté 12 juin 2010 - 06:07
gordonbrown82 wrote...
but doesn't that mean that if i have a string like "let's eat some candy and some popcorn"
it would reurn eat because that's after the first space? wouldn't everything get shopped up into too small pieces?
On a general note (and not in this code in particular) that depends on the length parameter, the last one. It could return a 3 chars long substring or everything between the specified index and the end of the string. To chop a string into a series of substrings, you'll have to look for an "explode" function. There is SplitString() available in DA scripts for that purpose, although it's commented as "do not use". (but maybe that comment isn't meant to be taken at heart)
Modifié par Phaenan, 12 juin 2010 - 06:08 .
#6
Posté 13 juin 2010 - 06:34
you useally only copy sections of a string into new strings leaving the old intact. if you want to get all the pieces you use a loop.





Retour en haut






