SRL Archive - Help

String manipulation


Bad Boy JH

2010-10-12 23:26:24

In all my time coding, I never really had to do much with string manipulation, however, I do need some help with it now. I have a string, which varies in length, and the bit I want is in the centre. This is an example of the string

have a chocolate bar for free.

I want the 'chocolate bar' section, which can change, the 'have a ' section and the ' for free.' section stay the same length ('have a ' is 7 and ' for free.' is 10).

how can I remove that middle portion of the string…

and for the record this is for a reflection - Sandwich Lady random solver.

Nava2

2010-10-12 23:28:50

Copy(pos('have a ', s), pos(' for free', s), s);

Bad Boy JH

2010-10-12 23:35:11

thanks

Edit: yeah, thats not quite right, for starters, the string is the first input into Copy, and that (when S is moved to the front gave me… 'have a chocolate bar ' - and yes there should be that space there…

Dgby714

2010-10-13 00:44:47

Copy(s, pos('have a ', s) + 7, pos(' for free', s) - 8);

Between('have a ', ' for free', s);

Cstrike

2010-10-13 00:53:45

Another thing you can do, which is not as efficient but:

Create a TStringArray of all the compiled phrases the woman says, and then just check them all with a for loop.

Ex:

function DetermineItemToPick : Integer;
var WhatSheSays : TStringArray; // Lolololol [sorry]
i : Integer;
LadyText : String;
begin
WhatSheSays := ['Have a chocolate bar for free.', 'have a whatever for free']; // etc...
LadyText := GetInterfaceText (parent, child); // Fill these in
result := -1;
for i:=0 to high (WhatSheSays) do
begin
if LadyText = WhatSheSays[0] then result := i;
end;
end;

function GetItemID (numberType : Integer) : Integer;
begin
if numberType = -1 then WarnUserTheyreScrewed;
case numberType of
0: result := 1233; // Whatever ID matches the phrase
1: result := 3494; // etc...
end;
end;

Then work it into a procedure.

Just an idea :)

i luffs yeww

2010-10-13 01:41:50

Or you could explode the string into a TSA and then read it like that. :3 I'd do that, personally, because it can be used for anything really. Read the string off the chat, explode it, and see if TSA + TSA (etc. etc.) == StringToMatch('whatever'). If yes, you've got it. :)

Cstrike

2010-10-13 11:59:32

From: i luffs yeww
Or you could explode the string into a TSA and then read it like that. :3 I'd do that, personally, because it can be used for anything really. Read the string off the chat, explode it, and see if TSA + TSA (etc. etc.) == StringToMatch('whatever'). If yes, you've got it. :)

How do you explode the string into a TSA?

Explode(string, TSA); ?

Also does that mean turn it into a singe letter like:

Explode ('lol',TSA) –> TSA['l','o','l'];

ZephyrsFury

2010-10-13 12:12:31


Explode(' ', 'Hi How are you today?');

Gives you an array of strings:


['Hi','How', 'are', 'you', 'today?']

The first parameter is the delimiter or the thing that separates your string into an array.


Explode(',', 'Hi,How,are,you,today?');

would have the same effect.

But yeah, most efficient way would be to use Between like how Dgby posted.

i luffs yeww

2010-10-13 19:36:26

But, in my opinion, the most fun thing would be exploding and imploding and what not. :3 And you'd learn a lot more, tbh.

ZephyrsFury

2010-10-13 19:42:18

From: i luffs yeww
But, in my opinion, the most fun thing would be exploding and imploding and what not. :3 And you'd learn a lot more, tbh.


We're talking about a random solver here, its not meant to be fun, its meant to be efficient.

i luffs yeww

2010-10-13 20:21:23

The difference in speed is negligible, in my opinion.

ZephyrsFury

2010-10-14 12:14:37

From: i luffs yeww
The difference in speed is negligible, in my opinion.


In this particular case perhaps, but in general practice: if you're coding for other people to implement into a bigger project, you want the code to efficient. No one really cares if you learnt anything from it. Just saying…