SRL Archive - OSR Help

Deleting values in strings and arrays


purerange86

2015-02-18 17:44:23

Is there a function to delete a specific index in an array in general?

for example I have the string

'1 1 1'

and i want to make it

'111'

For the purposes of this, is there a function that tells you whether a char is an integer or not?

What I have so far:

Procedure StringNoSpaces(var S:String);
var
i:integer
begin
for i := high(string) downto low(string) do begin
if (string[i] = ' ') then //deletevalueinstring(s, i) <--how are you supposed to do this
//what I really want is:
//if Not IsInteger(string[i]) then deletevalueinstring(s,i)
//but no clue what functions to use
end;
end;

Thanks!

3Garrett3

2015-02-18 17:47:25

From: purerange86
Is there a function to delete a specific index in an array in general?

for example I have the string

'1 1 1'

and i want to make it

'111'

For the purposes of this, is there a function that tells you whether a char is an integer or not?

Thanks!


The way that I know how to do it is follows:


myString := replace(myString, ' ', '', [rfReplaceAll]);

That will change "1 1 1" to "111"

Edit:

Didn't see the last part. I'm not sure if there's a way to tell if it's an integer or not. You could try


if strToInt(myStr) <> 0 then //It is integer

But it might just throw an error. So maybe something like


try
int := strToInt(myStr);
except
writeln('threw an error');
end;
if int <> 0 then // it is integer

I can't remember the exact syntax of Try, Except, Finally. But along those lines.

purerange86

2015-02-18 17:56:57

Thanks for the reply–
'1 1 1' is an arbitrary value, just to clarify, what I'm actually trying to do is find spaces and delete them.
What I've got so far:

Procedure StringNoSpaces(var S:String);
var
i:integer
begin
for i := high(string) downto low(string) do begin
if (string[i] = ' ') then //deletevalueinstring(s, i) <--how are you supposed to do this
end;

end;



3Garrett3

2015-02-18 18:03:53

From: purerange86
Thanks for the reply–
'1 1 1' is an arbitrary value, just to clarify, what I'm actually trying to do is find spaces and delete them.
What I've got so far:

Procedure StringNoSpaces(var S:String);
var
i:integer
begin
for i := high(string) downto low(string) do begin
if (string[i] = ' ') then //deletevalueinstring(s, i) <--how are you supposed to do this
end;

end;




You're treating a string as an "array of chars" which I mean technically it is, but you don't need to make it complicated.

Consider the following:


myString := 'Hello purerange86 this is an explanation';
Writeln(myString); // Gives "Hello purerange86 this is an explanation"

myString := replace(myString, ' ', '', [rfReplaceAll]);
Writeln(myString); // Gives "Hellopurerange86thisisanexplanation"

So the code I showed you will actually delete all spaces in a string. The arguments are something like:

replace(String, toReplace, replaceWith, Filter). So it's replacing a space with nothing ('') and the filter is replaceAll. I don't know where to find what the other filters are, but I'm sure someone might pop in and explain it.

purerange86

2015-02-18 18:07:33

Ohhh I get it now! I didn't understand initially what the replace thing did. Thanks for this and I will for sure use it!

Just for curiosity's sake, I found a procedure delete(array, i, i2) with three arguments. Anyone know how to use this and what the second and third arguments are?

Here's what I came up with, treating things as arrays; this worked.

begin
cts2go(0.00,2.02);
//s := (gettextat(17,44,0,5,4,10526943,35,7, 'nmzchars'));
s := '1 1 1';
j := 0;
for i := high(s) downto low(s) do
try
if strtoint(s[i]) > -1 then s2 := s2 + s[i];
except
end;
writeln(s2);
wait(5000);
end;

Olly

2015-02-18 18:10:04

var
s = '124 ffgfdg 653 1 5 6 1 66 abv 7 71544 4 656erwsfraefsf 6565 63 5563 dsfasdfsdaf dsafdfsa sddd ';

function Foo(const s: String): String;
var
i: UInt32;
begin
for i := 1 to Length(s) do
if (s[i] in ['0'..'9']) then
Result := Result + s[i];
end;


begin
Writeln(Foo(s));
// or
Writeln(ExtractFromStr(s, Numbers));
end.

Brandon

2015-02-18 19:58:26

To delete from a String:


Delete(SomeString, StartIndex, AmountOfCharactersToDelete); //Internal function in Simba.

To delete from an Array:


//Custom function.
Procedure Delete(var TIA: TIntegerArray; Index: Integer; Amount: Integer);
Begin
MemMove(TIA[Index + Amount], TIA[Index], (Length(TIA) - Amount) * sizeof(TIA[0]));
SetLength(TIA, Length(TIA) - Amount);
End;

Olly

2015-02-18 20:04:42

From: Brandon
To delete from a String:

To delete from an Array:


//Custom function.
Procedure Delete(var TIA: TIntegerArray; Index: Integer; Amount: Integer);
Begin
MemMove(TIA[Index + Amount], TIA[Index], (Length(TIA) - Amount) * sizeof(TIA[0]));
SetLength(TIA, Length(TIA) - Amount);
End;


Delete in lape is one of them 'magic' functions that work for any array, like Insert, Copy.

Brandon

2015-02-18 20:07:32

From: Olly
Delete in lape is one of them 'magic' functions that work for any array, like Insert, Copy.


Ahh turns out this is true. I didn't even test to see if the internal function works for Arrays. Just assumed it was strings only.. I think everyone except for me knew this -__-

@OP, you can use the same function for both:


Delete(SomeString, StartIndex, AmountOfCharactersToDelete); //Internal function in Simba.
Delete(SomeArray, StartIndex, AmountToDelete); //Internal function in Simba.


Still nice to see a small and fast implementation though.