Link to home
Start Free TrialLog in
Avatar of fluked
fluked

asked on

String Handling Routines

so far im reading from a file into an array using the basic AssignFile(infile,tempString) ... and load it into a string array, how am i able to get rid of spaces in between each word and display/save it to another array?

input: today is a nice day
output: todayisaniceday

-fluked
Avatar of BorlandMan
BorlandMan


to remove the blank spaces at the front of the tempstring, use TrimLeft()

if there will be spaces at the end of the string, you can remove the spaces of both by using the Trim() function

newStr := Trim(myLineFromFile);

to remove the spaces inbetween words you could use a function called Pos() to find the position of the next whitespace (if that is the delimiter of the words).

ex:

sample line:  '     Hello   World  Out There '

to get rid of whitespace on outside you use trim

ex: line := Trim(myLineFromFile);

then you use pos() to find out where the next whitespace is.

WsSpacePos := pos (' ', line);

use the function Copy() to copy from 1, to WsSpacePos and that gives you the first word

then you can essentially delete the word you just read using     delete(s, index, count)

then get the pos of the next whitespace again and keep on looping across each word until you hit the end... which you'll have to handle differently, since there won't be any whitespace to find after the last word... unless you do your Trim() after you read your words.

hope that gets you started.

sincerely,
J




ASKER CERTIFIED SOLUTION
Avatar of emadat
emadat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kretzschmar
or

function GetRidOfSpaces(AString : String) : String;
var i : integer;
begin
  result := '';
  for i := 0 to length(Astring) do
    if AString[i] <> ' ' then
      result := result + AString[i];
end;

just as alternative

meikl ;-)  
humm,

this should
for i := 0 to length(Astring) do

be changed to
for i := 1 to length(Astring) do
   
meikl ;-)
Hi,
I don't have much to add here, but there is another way to do this :

procedure TForm1.Button1Click(Sender: TObject);
var
  InputLine, SingleLine : string;
  ST : TStringList;
  i : integer;
begin
  ST:=TStringList.Create;
  try
    InputLine:='today is a nice day';
    SingleLine:=StringReplace(Trim(InputLine), ' ', chr(13)+chr(10), [rfReplaceAll, rfIgnoreCase]);
    ST.Text:=SingleLine;
    Memo1.Clear;
    for i:=0 to ST.Count-1 do
    begin
      if Trim(ST[i]) <> '' then
        Memo1.Lines.Add(ST[i]);
    end;
    //
  finally
    ST.Free;
  end;
end;

This code separate the words and save each word in a string (TStringList.Strings). In this sample i write the result to a memo.
In case you want to remove double spaces, try emadats answer with single space replacing double spaces :


//*********************************************************
var
     S1 : String;
    p : integer;
begin
    S1 := 'Today   is a    nice day';
    p := Pos('  ', s1);
    while P>0 do
    begin
      S1 := StringReplace(S1, '  ', ' ', [rfReplaceAll]);
      p := Pos('  ', s1);
    end;
    ShowMessage( s1);
end;

//*********************************************************


try this :


procedure TForm1.Button1Click(Sender: TObject);
var S: String;
begin
  S:=Edit1.Text;
  while Pos(' ', S) > 0 do
    Delete(S, Pos(' ', S), 1);
  Edit1.Text:=S;
end;


the above is just an hint ... so u cud apply the logic to read from the file!