Log in

View Full Version : String manipulation



Bad Boy JH
10-13-2010, 03:26 AM
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
10-13-2010, 03:28 AM
Copy(pos('have a ', s), pos(' for free', s), s);

Bad Boy JH
10-13-2010, 03:35 AM
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
10-13-2010, 04:44 AM
Copy(s, pos('have a ', s) + 7, pos(' for free', s) - 8);

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

Cstrike
10-13-2010, 04:53 AM
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
10-13-2010, 05:41 AM
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[i] + TSA[i + 1] (etc. etc.) == StringToMatch('whatever'). If yes, you've got it. :)

Cstrike
10-13-2010, 03:59 PM
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[i] + TSA[i + 1] (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
10-13-2010, 04:12 PM
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
10-13-2010, 11:36 PM
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
10-13-2010, 11:42 PM
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
10-14-2010, 12:21 AM
The difference in speed is negligible, in my opinion.

ZephyrsFury
10-14-2010, 04:14 PM
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...