Solved

creating string filled with spaces

Posted on 1998-11-08
5
201 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
[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
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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