?
Solved

how to break column in my text file ?

Posted on 2009-04-02
3
Medium Priority
?
194 Views
Last Modified: 2012-05-06
I have a text file very large, the problem is that my text has one line with a very long column.

I want to break column to another line after 80 caracter.

How can I do this?

Thanks
0
Comment
Question by:hidrau
[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
3 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 24051857
Is this a delimited (CSV, tab, other) text file?

How are you defining a 'column'?

Where do you need to place the results of your 'broke' column?

=====================
The general algorithm is to iterate through the length of the string, copying the chunks to another data structure, such as a TStringList.  With each iteration, you find the break point by starting 80 characters beyond your current position and inspect the characters 'backwards' (79,78,77, etc.) until you reach a word delimiter.  A word delimiter will typically be a space or hyphen.
0
 
LVL 13

Accepted Solution

by:
ThievingSix earned 2000 total points
ID: 24051951
I used this code for wrapping text to 55 characters in a text file. Might be useful to you.
function TrimSpaces(Text: String): String;
begin
  Result := Text;
  While Result[1] = #32 Do Delete(Result,1,1);
  While Result[Length(Result)] = #32 Do Delete(Result,Length(Result),1);
end;
 
function StrCharPos(Text: PChar; Chars: TSysCharSet): PChar;
begin
  Result := Text;
  while Result^ <> #0 do
  begin
    if Result^ in Chars then
      Break;
    Inc(Result);
  end;
  if Result^ = #0 then
    Result := nil;
end;
 
function WrapText(const Line, BreakStr: string; BreakChars: TSysCharSet;
  MaxCol: Integer): string;
var
  P, PNewLine, PBreak: PChar;
begin
  Result := '';
  P := PChar(Line);
  PNewLine := P;
  PBreak := StrCharPos(P, BreakChars);
  while PBreak <> nil do
  begin
    if (PBreak - PNewLine + 1) > MaxCol then
    begin
      Result := Result + BreakStr;
      PNewLine := P;
    end;
    while P <= PBreak do
    begin
      Result := Result + P^;
      Inc(P);
    end;
    PBreak := StrCharPos(PBreak + 1, BreakChars);
  end;
  if StrEnd(P) - PNewLine > MaxCol then
    Result := Result + BreakStr;
  while P^ <> #0 do
  begin
    Result := Result + P^;
    Inc(P);
  end;
end;
 
procedure TfrmMain.btnFormatClick(Sender: TObject);
var
  Buffer : String;
  StringList : TStringList;
  FormatList : TStringList;
  I, J : Integer;
begin
  StringList := TStringList.Create;
  Try
    Buffer := mData.Text;
    While Pos(#13#10#13#10,Buffer) > 0 Do
      begin
      StringList.Add(Copy(Buffer,1,Pos(#13#10#13#10,Buffer) - 1));
      Buffer := Copy(Buffer,Pos(#13#10#13#10,Buffer) + 4);
    end;
    StringList.Add(Buffer);
    FormatList := TStringList.Create;
    Try
      For I := 0 To StringList.Count - 1 Do
        begin
        FormatList.Text := StringList.Strings[I];
        For J := 0 To FormatList.Count - 1 Do
          begin
          If Length(FormatList.Strings[J]) > 55 Then
            begin
            FormatList.Text := StringReplace(FormatList.Text,#13#10,#32,[rfReplaceAll]);
            FormatList.Text := WrapText(FormatList.Text,#13#10,[#32],55);
            Break;
          end;
        end;
        For J := 0 To FormatList.Count - 1 Do
          begin
          FormatList.Strings[J] := Trim(FormatList.Strings[J]);
        end;
        StringList.Strings[I] := FormatList.Text;
      end;
    Finally
      FreeAndNil(FormatList);
    end;
  mData.Text := StringReplace(StringList.Text,#13,'',[rfReplaceAll]);
  Finally
    StringList.Free;
  end;
end;

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 31565845
thanks very much
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month13 days, 7 hours left to enroll

801 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