Aller au contenu

Photo

what is a substring?


  • Veuillez vous connecter pour répondre
5 réponses à ce sujet

#1
gordonbrown82

gordonbrown82
  • Members
  • 544 messages
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 + " ";

#2
Phaenan

Phaenan
  • Members
  • 315 messages
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.

Modifié par Phaenan, 12 juin 2010 - 05:34 .


#3
gordonbrown82

gordonbrown82
  • Members
  • 544 messages
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?

#4
ChewyGumball

ChewyGumball
  • Members
  • 282 messages
It only displays once the concatenated string is longer than 37 characters or there is nothing left to concatenate.

#5
Phaenan

Phaenan
  • Members
  • 315 messages

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
CID-78

CID-78
  • Members
  • 1 124 messages
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.