Solved

creating string filled with spaces

Posted on 1998-11-08
5
191 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
ID: 1346183
SetLength(TheString, TheLength);
fillchar(TheString[0], TheLength,  ' ');

Cheers,

Raymond.


0
 
LVL 10

Expert Comment

by:viktornet
ID: 1346184
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
ID: 1346185
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
ID: 1346186
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
ID: 1346187
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

910 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

20 Experts available now in Live!

Get 1:1 Help Now