Log in

View Full Version : Removing text from a string



HT BaaFly
11-19-2011, 10:10 AM
Working on my first script but couldn't get past this obstacle:
I'm trying to remove a peice of text from a string. e.g.:
I've got the string "1 2 g p" and I'm trying to remove all the text from it and end up with the numbers so I can convert it to an integer and use it for something else. I'm trying to figure out how to remove the " g p" part. Is there an already made function in SRL to do this? if not, can someone give me some hints of how to achive it

thanks a lot

edit:
program new;
var
text: string;
int: integer;
begin
text := GetTextAtEx(296, 186, 440, 199, 0, 0, 0, 6400255, 5, 'UpChars');
int := StrToInt(text)
Writeln(int);
end.

this is the code for retrieving the text, if anybody is interested

HT BaaFly
11-19-2011, 10:54 AM
I've discovered the replace function but I don't know how to use it yet :S

program new;
var
text, text1: string;
int: integer;
begin
text := GetTextAtEx(296, 186, 440, 199, 0, 0, 0, 6400255, 5, 'UpChars');
text1 := Replace(text, ' g p', '1');
int := StrToInt(text1)
Writeln(int);
end.

Nava2
11-19-2011, 12:58 PM
GetNumbers!

Can't find any reference guide at the moment, but its straight forward. :)

HT BaaFly
11-19-2011, 01:10 PM
Made my function for finding the numbers but the problem with it is that it can't detect numbers above 999 for some reason :S

Here's the script so you can have a look at it:

program new;

procedure Findnumber;
var
text, text1, text2: string;
int: integer;

begin
text := GetTextAtEx(296, 186, 440, 199, 0, 0, 0, 6400255, 5, 'UpChars');
text1 := Replace(text, ' g p', '', []);
text2 := Replace(text1, ' ', '', [rfReplaceAll]);
int := StrToInt(text2);
Writeln(int);
end;

begin
ClearDebug;
Findnumber;
end.

It always gives me "Error: Exception: "1000" is an invalid integer at line 10" with numbers 1000 or above

Anyway, I'll try the Getnumber you sent me :)

Home
11-19-2011, 01:36 PM
function TrimLetters(s : string) : string;
var
LetterArray: TStringArray;
i: Integer;
begin
LetterArray := ['a','b','c','d','e','f','g','h','i','j','k','l','m ','n','o',
'p','q','r','s','t','u','v','w','x','y','z', '.'];
result := s;
for i := 0 to Length(LetterArray) - 1 do
result := Replace(result, LetterArray[i], '', [rfReplaceAll, rfIgnoreCase]);
end;

begin
Writeln(TrimLetters('619237109 x Coins.'));
end.


Try that one.

~Home

KingKong
11-19-2011, 01:56 PM
Use GetTextAtExWrap and see if that works. It works for me with numbers >999
Or try changing the min spacing like so:

StrToInt(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars'));

HT BaaFly
11-19-2011, 01:57 PM
That solved it. Thanks a lot, Homer :)

program new;
var
text, text1: string;
int: integer;

function TrimLetters(s : string) : string;
var
LetterArray: TStringArray;
i: Integer;
begin
LetterArray := ['a','b','c','d','e','f','g','h','i','j','k','l','m ','n','o',
'p','q','r','s','t','u','v','w','x','y','z', '.', ',', ' '];
result := s;
for i := 0 to Length(LetterArray) - 1 do
result := Replace(result, LetterArray[i], '', [rfReplaceAll, rfIgnoreCase]);
end;


begin
ClearDebug;
text := GetTextAtEx(296, 186, 440, 199, 0, 0, 0, 6400255, 5, 'UpChars');
text1 := TrimLetters(text);
int := StrToInt(text1);
Writeln(int);
end.

But now I'm left with another problem; I get this error when I try to include SRL in the script


[Error] (18:80): Invalid number of parameters at line 17
Compiling failed.

program new;
{$i SRL/SRL.scar}
{$i srl/srl/misc/grandexchange.scar}
var
text, text1: string;
int: integer;

function TrimLetters(s : string) : string;
var
LetterArray: TStringArray;
i: Integer;
begin
LetterArray := ['a','b','c','d','e','f','g','h','i','j','k','l','m ','n','o',
'p','q','r','s','t','u','v','w','x','y','z', '.', ',', ' '];
result := s;
for i := 0 to Length(LetterArray) - 1 do
result := Replace(result, LetterArray[i], '', [rfReplaceAll, rfIgnoreCase]);
end;


begin
SetupSRL;
ClearDebug;
text := GetTextAtEx(296, 186, 440, 199, 0, 0, 0, 6400255, 5, 'UpChars');
text1 := TrimLetters(text);
int := StrToInt(text1);
Writeln(int);
end.

The same also happens with the GetTextAtEx(....) function. It seems that both simba and SRL have the same function and there is now way that I know off to tell the scipt which function to use :S

HT BaaFly
11-19-2011, 01:59 PM
Use GetTextAtExWrap and see if that works. It works for me with numbers >999
Or try changing the min spacing like so:

StrToInt(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars'));

I'll try it and see if it works with SRL included

Echo_
11-19-2011, 02:00 PM
Just rename TrimLetters

E: Heres mine :D
function GetNumerals(const s: string): string;
var
i, j: Integer;
numArray: array of string;
begin
numArray := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
for i := 1 to High(s) do
for j := 0 to High(numArray) do
begin
if not (s[i] = numArray[j]) then Continue;
Result := Result + s[i];
end;
end;

HT BaaFly
11-19-2011, 02:10 PM
Just rename TrimLetters

The problem is with the replace function inside TrimLetters. Tried changing the name of Trimletters and got the same result :(


@KingKong: Tried what you told me and it worked but as soon as I included SRL it gave me the same error as before.

program new;
{$i SRL/SRL.scar}
{$i srl/srl/misc/grandexchange.scar}


procedure Findnumber;
var
text, text1: string;
int: integer;

begin
text := GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars');
text1 := Replace(text, 'gp', '', []);
int := StrToInt(text1);
Writeln(int);
end;

begin
SetupSRL;
ClearDebug;
Findnumber;
end.


[Error] (14:39): Invalid number of parameters at line 13
Compiling failed.


fixing this is beyond my expertise :frusty:



Edit: Thanks to everyone for helping, I've just fixed it :):):):):):)

program new;
{$i SRL/SRL.scar}
{$i srl/srl/misc/grandexchange.scar}
var
text, text1: string;
int: integer;

function TrimLetters(s : string) : string;
var
LetterArray: TStringArray;
i: Integer;
begin
LetterArray := ['a','b','c','d','e','f','g','h','i','j','k','l','m ','n','o',
'p','q','r','s','t','u','v','w','x','y','z', '.', ',', ' '];
result := s;
for i := 0 to Length(LetterArray) - 1 do
result := Replace(result, LetterArray[i], '');
end;


begin
ClearDebug;
SetupSRL;
text := GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars');
text1 := TrimLetters(text);
int := StrToInt(text1);
Writeln(int);
end.

Echo_
11-19-2011, 02:14 PM
The problem is with the replace function inside TrimLetters. Tried changing the name of Trimletters and got the same result :(


@KingKong: Tried what you told me and it worked but as soon as I included SRL it gave me the same error as before.

program new;
{$i SRL/SRL.scar}
{$i srl/srl/misc/grandexchange.scar}


procedure Findnumber;
var
text, text1: string;
int: integer;

begin
text := GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpChars');
text1 := Replace(text, 'gp', '', []);
int := StrToInt(text1);
Writeln(int);
end;

begin
SetupSRL;
ClearDebug;
Findnumber;
end.


[Error] (14:39): Invalid number of parameters at line 13
Compiling failed.


fixing this is beyond my expertise :frusty:

Try mine since it doesn't use replace.

HT BaaFly
11-19-2011, 02:26 PM
Alright, thanks :) turned out all I had to do was remove the '[]' in the replace function xD that was the only difference between the SRL and the simba versions lol