Solved

creating string filled with spaces

Posted on 1998-11-08
5
188 Views
Last Modified: 2011-09-20
2. What is optimal way to create specified length string filled with spaces.

Following code doesn't look too elegant:

Text := '';    
for i := 1 to Specified_length do Text := Text + ' ';
0
Comment
Question by:yuhoo
5 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
Comment Utility
SetLength(TheString, TheLength);
fillchar(TheString[0], TheLength,  ' ');

Cheers,

Raymond.


0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
Hey Raymond... I think you cannot use String[0] even though the length of the string is kept there..

Cheers,
Viktor
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi yuhoo,

your for-statement is the solution,why do you search a other solution?
you can also do it in a while or repeat loop, bat the result is just the same.

meikl

0
 
LVL 1

Expert Comment

by:BlackDeath
Comment Utility
hi, yuhoo.

function FillString(s: string; c: Char): string;
var
  a: array[1..255] of Char;
begin
  FillChar(a, SizeOf(a), c);
  Result := a;
  SetLength(Result, Length(s));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  s := '123';
  Edit1.Text := FillString(s, '*');
end;

this doesn't look nice either, eh?

sorry, bye

Black Death.
0
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Oops... you are right viktornet, it should be TheString[0] (I was also assuming huge strings here so it wouldn't have compiled!). It will also work for short strings provided the short string has been declared sufficiently long enough (up to 255 chars) that it does not overrrun the end of it.

Cheers,

Raymond.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now