Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

creating string filled with spaces

Posted on 1998-11-08
5
Medium Priority
?
204 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

610 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