• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

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
0
fluked
Asked:
fluked
1 Solution
 
BorlandManCommented:

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




0
 
emadatCommented:

Try this:

//**********************************************************
var
     S, S1 : String;
begin
     S := 'Today is a nice day';
     S1 := StringReplace(S, ' ', '', [rfReplaceAll]);
//**********************************************************

Regards,
0
 
kretzschmarCommented:
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 ;-)  
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kretzschmarCommented:
humm,

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

be changed to
for i := 1 to length(Astring) do
   
meikl ;-)
0
 
PeterLarsenCommented:
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.
0
 
TeunSpaansCommented:
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;

//*********************************************************
0
 
nafeelmCommented:


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;

0
 
nafeelmCommented:

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now