?
Solved

String Handling Routines

Posted on 2003-03-27
8
Medium Priority
?
150 Views
Last Modified: 2010-04-04
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
Comment
Question by:fluked
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Expert Comment

by:BorlandMan
ID: 8222386

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
 
LVL 3

Accepted Solution

by:
emadat earned 60 total points
ID: 8222694

Try this:

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

Regards,
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8222778
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 8222961
humm,

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

be changed to
for i := 1 to length(Astring) do
   
meikl ;-)
0
 
LVL 2

Expert Comment

by:PeterLarsen
ID: 8223470
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
 

Expert Comment

by:TeunSpaans
ID: 8223481
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
 
LVL 1

Expert Comment

by:nafeelm
ID: 8225150


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
 
LVL 1

Expert Comment

by:nafeelm
ID: 8225168

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question