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
Solved

creating string filled with spaces

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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