Log in

View Full Version : Lokking for a way to trim spaces from a string.



WT-Fakawi
02-18-2012, 10:09 AM
program test;
var
s:String;
begin
s:= 'This is a test';
Trim(s);
Writeln(s);
end.

I was assuming Trim removes spaces, but appearantly, I am doing something wrong.

I need this because I want to convert a string to a number '691 coins have been added to your pouch' or '691coins have been added to your pouch' (sometimes the space is gone) and keep 691. Got it working, except for the spaces error

program test;

var
text: String;
a, Number: Integer;

procedure Grabnumbers;

begin
text := '123 coins have been';
a := Pos('coins', text);
writeln('a = ' + IntToStr(a));
SetLength(Text, a - 2);
Writeln('text = ' + Text +']');
Trim(Text);
Writeln('trim = ' + Text +']');
Try
Number := StrToInt(Text)
Except // nothing for now, resulting in 0
end;
writeln('num = ' + IntToStr(Number));
end;

begin
Grabnumbers;
end.



tl: dr; How do you use Trim?

weequ
02-18-2012, 10:27 AM
Is this what you are looking for? Numbers(String);

Also to get rid of spaces try this: Replace(String, ' ', '', rfReplaceAll);


Trim only removes spaces at the beginning and end of the string.

JuKKa
02-18-2012, 10:36 AM
tried "GetNumbers(string): string;?"

DemiseScythe
02-18-2012, 12:25 PM
ExtractFromStr('s', Numbers); ^.^ also put inside StrToInt and you got yourself the number of coins ^.^~

i := StrToInt(ExtractFromStr('s', Numbers));

bg5
02-18-2012, 07:51 PM
Idk much about inbuild functions so i made my own.

program test;

function GrabNumber(str : string) : integer;
var
a,l : integer;
tempStr : string;
begin
l := High(str);
tempStr := '';
for a:= 1 to l do
begin
if (ord(str[a])>47)and(ord(str[a])<58) then
tempStr := tempStr + str[a];
end;
if (High(tempStr)>0) then
Result := strtoint(tempStr) else
Result := -1; // or whatever
end;


var
s:String;
begin
s:= '691 coins have been added to your pouch';
writeln ('Money in pouch : '+tostr(GrabNumber(s)));
end.
Output

Money in pouch : 691

BTW. Just for trim spaces:

program test;

function SpaceTrimmer(str : string) : string;
var
a,l : integer;
begin
l := High(str);
Result := '';
for a:= 1 to l do
begin
if (ord(str[a])<>32) then
Result := Result + str[a];
end;

end;


var
s:String;
begin
s:= '691 coins have been added to your pouch';
writeln (SpaceTrimmer(s));
end.
Output:

691coinshavebeenaddedtoyourpouch

Brandon
02-18-2012, 08:00 PM
If u fancy regex's, u can just do ExtractRegExpr(string, '\s').. all spaces gone. Not only that but it can tell u if the string contains numbers or letters or characters or a combination of all and remove all in one go.

Dgby714
02-18-2012, 08:42 PM
program new;

function GetNumbers(const x: string): Extended;
begin
Result := StrToFloatDef(ReplaceRegExpr('[^\d+-\.]*([-+]?[0-9]*\.?[0-9]+)[^\d]*', x, '$1', True), 0);
end;

begin
WriteLn(GetNumbers('600')); //600
WriteLn(GetNumbers('-600')); //-600
WriteLn(GetNumbers('1.2')); //1.2
WriteLn(GetNumbers('-1.2')); //-1.2
WriteLn(GetNumbers('.2')); //0.2
WriteLn(GetNumbers('-.2')); //-0.2
WriteLn(GetNumbers('1.')); //1
WriteLn(GetNumbers('-1.')); //-1

WriteLn(GetNumbers('')); //0
WriteLn(GetNumbers('Hello'));//0
WriteLn(GetNumbers('l33t')); //33
end.