(SILLY) Problem with "contains" (IS SOLVED)

Started by Rui 'Trovatore' Pires, Sun 11/11/2007 14:03:37

Previous topic - Next topic

Rui 'Trovatore' Pires

If this is some oversight of mine, I'll feel really stupid...

Ok, so I have the following code:

Code: ags
    String tempsyn=Room.GetTextProperty("RoomExtraSynon");
    if (tempsyn!="") {
      lstHiddenObj.Clear();
      while (tempsyn.Contains(".")) {
        Display(tempsyn);
        lstHiddenObj.AddItem(tempsyn.Truncate(tempsyn.Contains(".")));
        tempsyn=tempsyn.Substring(tempsyn.Contains(".")+1, tempsyn.Length);
        Display(tempsyn);
        if (tempsyn.Contains(".")) Display("Yes");
      } 
      //Display(tempsyn);
    }


Basically, there's a property in each room. In this case, the property reads "opening.desk.stool". The idea is, get each of these words and put them in lstHiddenObj.

It didn't work for quite a while, and eventually I added the Displays to see what was going on. I noticed that even when tempsyn was down to "stool", it still displayed "Yes" and it still triggered the while... which of course errored out.

I tried changing the periods to commas, both in the property (opening,desk,stool) and in the code.

It didn't change anything, so I switched back to periods.

Now here's the interesting bit - nothing changed. It processed the first two items, checking for periods, and stopped at the end because it seemed to find a period somewhere in "stool".

Why is it interesting? Because I'd forgotten to change the property back. It was checking "opening,desk,stool" for periods, and FINDING them, where there are commas.

Well, any thoughts?

EDIT - And I do know that I might have to include an extra line after the while in order to include the last word, the one with no more commas or periods, but until I solve this, there's little point...
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

GarageGothic

#1
Shouldn't it be:
Code: ags
tempsyn=tempsyn.Substring(tempsyn.Contains(".")+1, tempsyn.Length-(tempsyn.Contains(".")+1));
?

Edit: Not sure about the +1, I always mix up the index/length thing and have to adjust after testing.

Rui 'Trovatore' Pires

No, Substring asks for the initial and end values. "Contains" gives me the first value, or "desk.stool", and "Length" tells me how far to go, i.e., just after "l". I think your suggestion would start and stop in the same char...
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

smiley

#3
Code: ags
while (tempsyn.Contains(".") != -1) 


Contains returns the position of the string or -1 if it didn't found it.
So the while-condition will only be false, if the string is at the first position (==0).

Rui 'Trovatore' Pires

See? I told you I'd feel really stupid.

I'll go re-check the whole thing...
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

GarageGothic

Isn't the point to cut off the first word of the String, including the "."?

Substring doesn't ask for two index values but for an index value and the length of the substring, which in this case should be the length of the original String minus the part you're cutting off.

Rui 'Trovatore' Pires

#6
Yeah, the point is to cut it off, so I it to return a substring withing the original one, and I want said substring to go from the "." to the end, hence the "length".

I'm cutting off the first word, not the last one.

EDIT - And of course, Smiley hit the problem right away. Heh, the only problem with "contains" is that it doesn't work quite as intuitively as it sounds. :) Thanks, smiley, you made me smiley!
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

GarageGothic

#7
But the length that you're currently using is the length of the current String, not of the substring. Perhaps AGS handles this by cropping the String to the right length without adding blank spaces or anything, so there's no problems, but the length you're supplying is indeed "wrong".

Rui 'Trovatore' Pires

#8
Yeah, I know.

opening.desk.stool = opening.desk.stool substring (start at first period, then go the rest of the line);

Which makes it desk.stool

Give or take the period, I always have to adjust +1 and -1 too.

EDIT - But how could I use the length of the substring, if it hasn't been created yet? I really don't follow.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

GarageGothic

#9
Since the problem seems to be solved, I'll just reply as future reference for others. You're not using the length of the created substring, you're calculating it by taking the length of the current String (tempsyn.Length) and subtracting the length of the part you're cutting off (tempsyn.Contains(".")+1).

What's currently happens in your code is that you're asking for a substring the same length as the original, but it seems that AGS is somehow correcting that, since it can't go beyond the end of the string anyway. (I'd actually be curious to know if it returns a String of the requested length by adding spaces at the end or not).

Rui 'Trovatore' Pires

I know... isn't that the way to do it? I'm not being purposefully dense, I'm just honestly not getting it. I'm turning the string into a new string, calculated from the original string.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

GarageGothic

Maybe I phrased it badly. What I meant was that to get the PROPER length of the created substring, you calculate it by taking the length of the current String (tempsyn.Length) and subtracting the length of the part you're cutting off (tempsyn.Contains(".")+1). Hence my suggestion of:

Code: ags
tempsyn=tempsyn.Substring(tempsyn.Contains(".")+1, tempsyn.Length-(tempsyn.Contains(".")+1));


What you're CURRENTLY doing is just taking the length of the original String, which is - I think we can agree on this - longer that the substring you want to create. So you're asking AGS to cut off the beginning of the String, but to keep the new String the same length as the original one, which doesn't make much sense.

Pumaman

Heh, in hindsight "Contains" was a rather poor name for this function because it does imply a true or false return value. Ah well, too late now :/

monkey0506

#13
Quote from: GarageGothic on Sun 11/11/2007 14:14:27Shouldn't it be:
Code: ags
tempsyn=tempsyn.Substring(tempsyn.Contains(".")+1, tempsyn.Length-(tempsyn.Contains(".")+1));
?

String.Substring will automatically return (at the end of the String) if the length parameter passed exceeds the length of the original String (AFAIK). Even if it doesn't return immediately, the returned String will still be truncated appropriately. Depending how the function handles the length parameter it may be faster to use "tempsyn.Length - (tempsyn.Contains(".") + 1" instead of just "tempsyn.Length", though both would result in the same (sub)String.

SMF spam blocked by CleanTalk