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

x
?
Solved

string spaces

Posted on 2001-07-20
45
Medium Priority
?
249 Views
Last Modified: 2010-04-06
i am looking for a routine that will remove any spaces from the begining of a string passed to it.

i need one that will be robust enough to work as i tried one i found on the internet and it didn't work 100%.

i'll award the points to the one that works the best.
0
Comment
Question by:Xperl
[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
  • 16
  • 11
  • 5
  • +7
45 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6303820
Have you tried the TrimLeft function

From Delhi Help

Trims leading spaces and control characters from a string.

Unit

Sysutils

Category

string handling routines

function TrimLeft(const S: string): string;

Description

TrimLeft returns a copy of the string S with leading spaces and control characters removed.


The Crazy One
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6303831
var
  s: string = ('    abc');

procedure TForm1.Button1Click(Sender: TObject);

begin

  ShowMessage(s + #13#10 + TrimLeft(s));

end;
0
 
LVL 3

Expert Comment

by:ahll
ID: 6303833
look this:

function RemoveSpaces(CompleteString: string): string;
var
 i: byte;
begin
  while CompleteString[i] = ' ' do  
  begin
    delete(CompleteString,1,1);
  end;
  result:= CompleteString;
end;

I hope this is what you are looking for.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 13

Expert Comment

by:Epsylon
ID: 6303922
:o/
0
 

Author Comment

by:Xperl
ID: 6304340
thanks ahll but i prefer to leave the question open as it is fair to others to do so.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6304596
ahll , i think u dont need (var i : byte) and [1] is insteed of [i]. ;-)
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6304673
Hey CrazyOne. You like these string questions don't you? :)

I think I'll let you sweat this one out.
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6304733
:-))) gemarti.
0
 

Expert Comment

by:felonious
ID: 6304879
TrimLeft sound like the answer to me
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6305397
Hi gemarti
yeah I guess I do. hehehe
0
 
LVL 1

Expert Comment

by:Dennis9
ID: 6306357
Hi.
Why not use Stringreplace?

It works like this:
memo2.text := StringReplace(Memo1.Text, ' ', '', [rfReplaceAll]);

It will search in Memo1 for spaces, and remove those and add it to memo2.

Dennis
0
 

Expert Comment

by:felonious
ID: 6306467
stringreplace would replace all the instances of the char, he just wanted to trim the leading spaces
0
 

Author Comment

by:Xperl
ID: 6307444
Thanks everyone for your input so far but i am looking for a robust routine to do the job without using the delphi trimleft command.  I want the program i am working on to be able to do it via code.  btw, i am using delphi 2.

any real good ideas?
0
 

Expert Comment

by:felonious
ID: 6308073
ok are there more guidelines? if not you could try this one.

function fTrimLeft(str : string) : string;
var
  I : Integer;

begin
  I := 0;
  While Str[1] = ' ' do Inc(I);
  result := copy(str,I,9999);
end;
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6308220
it seems Str[1] must be str[i] .

also 'i' point to last space char therefor
result := copy(str,I+1,9999); it true.

and I think this code has problem for str that has not eny space in the first use :
if i > 0 then :
  result := ...
else
  result := str;
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6308239
maybe it is better :

 I := 1;
 While (Str[i] = ' ') do Inc(I);
// now i point to first nonspace char

but i think  ahll code ( after correction his little mistake ) is the best.becouse for example ur code has problems with str := '        ' . i countines more than length(str).

best regards
hamed
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6308249
function atrimleftfunc(Astring : String;ACharSet : Set Of Char) : String;
begin
  result := AString;
  while (Length(Result)) > 0) and (Result[1] in ACharset) do
    delete(result,1,1);
end;

to call (for ex)
s := atrimleftfunc(s,[#0..#32]);


not tested, just from head, typos possible, but should work also with turbo pascal (if needed)

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6308261
hmm, for turbo pascal a bit modifcation must be done,
because it supports not the result-automatism from delphi

function atrimleftfunc(Astring : String;ACharSet : Set Of Char) : String;
var result : String;
begin
 result := AString;
 while (Length(Result)) > 0) and (Result[1] in ACharset) do
   delete(result,1,1);
 atrimleftfunc := result
end;

;-)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6308264
hi meikl,

i can not recieve your replay yet. i am still waiting for that.is there any problem?

best regards
hamed
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6308283
the problem, hamed, is:
i have no time, thats all.
well, i should take a quarter hour
to complete my reply to you ;-)

be patient with me

meikl ;-)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6308328
Yes Sir! ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6308390
:-)
0
 

Expert Comment

by:felonious
ID: 6309896
thank you hamed for correcting my code.  ofcourse the way I wrote it would not work :)

-felonious
0
 
LVL 3

Expert Comment

by:Sabre
ID: 6310235
Thought i would throw in my two cents worth too.   This one should be pretty straight forward and robust to do the job.

Function TrimBlanksLeft(S: String): String;
Var
  i : Integer;
Begin
  For i := 1 To Length(S) Do
  Begin
    If S[i] = ' ' Then
    Begin
      Delete(S,1,1);
    End
    Else
    Begin
      Break;
    End;
  End;
  Result := S;
End;

0
 

Expert Comment

by:felonious
ID: 6310822
Sabre, yeah the only problem with that is the variable I will keep increasing as the length(s) gets shorter, so you will probably get errors, like I is greater then the length of S.


-felonious
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6310946
Sabre ,i am agree with felonious ( my friend ) after Delete the first space you lose the length, that means after each delete u must refresh the for loop to make it compatible with new length! ( becouse delete change the length ) &as felonious said i pass the length(S).

best regards
hamed

   
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6310949
now what is ur idea about the following code that is tha same ahll code after correction :


function RemoveSpaces(CompleteString: string): string;
begin
 while CompleteString[1] = ' ' do  
 begin
   delete(CompleteString,1,1);
 end;
 result:= CompleteString;
end;

0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6310957
oh, i add a new line :

function RemoveSpaces(CompleteString: string): string;
begin
while CompleteString[1] = ' ' do  
begin
  delete(CompleteString,1,1);
  if CompleteString='' then Break; // for just space string
end;
result:= CompleteString;
end;
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6311057
I am curious if Delphi 2 has the Delete procedure? if it does then h_mohsenian you almost have it. The decleration just needs to be changed to this

function RemoveSpaces(var CompleteString: string): string;

instead of this
function RemoveSpaces(CompleteString: string): string;

0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6311173
hi CrazyOne ,

1. I checked ( from a Document ). D2 has 'Delete' ( in the same format of D3,.. ).

2.i can not underestand the couse of ur suggestion!becouse it is a func. and have 'Result' , maybe D2 have not 'Result' def. , if it was true that is better he use :
RemoveSpaces := CompleteString; ( at the end )

3.I wana add another line for pass empty strings, therefore we can rewrite the func. as follow :

function RemoveSpaces(S: string): string;
begin
if S <> '' then // solve empty String problem
  while S[1] = ' ' do  
  begin
    delete(S,1,1);
    if S='' then Break; // for just space string
  end; // while
result:= S;
end;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6311332
seems no one takes a look to my sample
just repeat the turbo pascal version

function atrimleftfunc(Astring : String;ACharSet : Set Of Char) : String;
var result : String;
begin
  result := AString;
  while (Length(Result)) > 0) and (Result[1] in ACharset) do
    delete(result,1,1);
  atrimleftfunc := result
end;

benefits
- empty strings are handled
- a variable set of chars can used for trimming

meikl ;-)
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6311376
hi meikl ,

it is a good func. but has some little problems becouse it is pascal Code :
- Delphi has problem with Set of Char. we must use this definition :
Type
 TMySet : Set of Char;
..
..
function ( .. ; .. : TMyChar ) : ..

- Result is Redecleared for Delphi . as u said it is Pascal Code.

- one Parantes is lost.

i am just ur student, but i think corrected Code For Delphi is :

type

TMySet : Set of Char;

function atrimleftfunc(Astring : String;ACharSet : TMySet) : String;
//var result : String;
begin
 result := AString;
while ((Length(Result)) > 0) and (Result[1] in ACharset) do
  delete(result,1,1);
 atrimleftfunc := result
end;


best regards
hamed
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6311387
a good fiture for this func. is that :

-it can used for trimleft any chars that u type in ACharSet for example for trim ' ' and '#' u can use this code :

S := atrimleftfunc(S,[' ','#');

hamed

0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6311395
a good fiture for this func. is that :

-it can used for trimleft any chars that u type in ACharSet for example for trim ' ' and '#' u can use this code :

S := atrimleftfunc(S,[' ','#']);

hamed

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6311461
:-) well haven't tested it

but if the Declaration Set Of Char is a problem,
then use TSystemCharset instead of this

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6311650
or was it TSysCharSet?
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6311877
yes TSysCharSet is true.but meikl is it from pascal source code ? i have never seen pascal source code! ( like Delphi )
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6311992
hi h_mohsenian

>>>2.i can not underestand the couse of ur suggestion!becouse it is a func. and have 'Result' , maybe D2 have not 'Result' def. , if it was true that is better he use : RemoveSpaces := CompleteString; ( at the end )

Well when I tested it didn't work until I declared the parameter as a variable. CompleteString never changed, perhaps it is because I was passing a variable to it instead of a raw string. :>)


The Crazy One
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 600 total points
ID: 6312000
no, i had it just in mind

well to make it complete

type TCharSet = Set of Char;

function atrimleftfunc(Astring : String;ACharSet : TCharSet) : String;
var result : String;
begin
 result := AString;
 while (Length(Result)) > 0) and (Result[1] in ACharset) do
   delete(result,1,1);
 atrimleftfunc := result
end;

(but not tested already, maybe this evening)

meikl ;-)

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316554
Xperl, whats going on?
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6333433
Xperl, are you alive?
0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6334532
hi meikl, maybe he solved his problem himself.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6335361
well, if so, hamed,
then xperl should say it
:-)
0
 

Author Comment

by:Xperl
ID: 6335827
Thanks all.  And sorry for the delay - was busy elsewhere.

Seesh, what a selection to choose from and all of them have their use.  I want to be fair here by selecting the answer that suits my needs but i also think i shall award points to two others (in other posts).

Meikl:  I am accepting your answer to this question.

hamed: i shall post another question with some points for your for your help in spotting all the errors in others code.

sabre: i shall post another question with some points for the "break" idea you gave - i never knew about that and since i saw you used it it has alowed me to make other code of mine a lot "cleaner".


0
 
LVL 1

Expert Comment

by:h_mohsenian
ID: 6336730
it is an interesting way!
0

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

636 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