• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

string spaces

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
Xperl
Asked:
Xperl
  • 16
  • 11
  • 5
  • +7
1 Solution
 
CrazyOneCommented:
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
 
CrazyOneCommented:
var
  s: string = ('    abc');

procedure TForm1.Button1Click(Sender: TObject);

begin

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

end;
0
 
ahllCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
EpsylonCommented:
:o/
0
 
XperlAuthor Commented:
thanks ahll but i prefer to leave the question open as it is fair to others to do so.
0
 
h_mohsenianCommented:
ahll , i think u dont need (var i : byte) and [1] is insteed of [i]. ;-)
0
 
gemartiCommented:
Hey CrazyOne. You like these string questions don't you? :)

I think I'll let you sweat this one out.
0
 
h_mohsenianCommented:
:-))) gemarti.
0
 
feloniousCommented:
TrimLeft sound like the answer to me
0
 
CrazyOneCommented:
Hi gemarti
yeah I guess I do. hehehe
0
 
Dennis9Commented:
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
 
feloniousCommented:
stringreplace would replace all the instances of the char, he just wanted to trim the leading spaces
0
 
XperlAuthor Commented:
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
 
feloniousCommented:
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
 
h_mohsenianCommented:
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
 
h_mohsenianCommented:
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
 
kretzschmarCommented:
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
 
kretzschmarCommented:
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
 
h_mohsenianCommented:
hi meikl,

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

best regards
hamed
0
 
kretzschmarCommented:
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
 
h_mohsenianCommented:
Yes Sir! ;-)
0
 
kretzschmarCommented:
:-)
0
 
feloniousCommented:
thank you hamed for correcting my code.  ofcourse the way I wrote it would not work :)

-felonious
0
 
SabreCommented:
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
 
feloniousCommented:
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
 
h_mohsenianCommented:
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
 
h_mohsenianCommented:
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
 
h_mohsenianCommented:
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
 
CrazyOneCommented:
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
 
h_mohsenianCommented:
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
 
kretzschmarCommented:
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
 
h_mohsenianCommented:
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
 
h_mohsenianCommented:
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
 
h_mohsenianCommented:
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
 
kretzschmarCommented:
:-) well haven't tested it

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

meikl ;-)
0
 
kretzschmarCommented:
or was it TSysCharSet?
0
 
h_mohsenianCommented:
yes TSysCharSet is true.but meikl is it from pascal source code ? i have never seen pascal source code! ( like Delphi )
0
 
CrazyOneCommented:
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
 
kretzschmarCommented:
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
 
kretzschmarCommented:
Xperl, whats going on?
0
 
kretzschmarCommented:
Xperl, are you alive?
0
 
h_mohsenianCommented:
hi meikl, maybe he solved his problem himself.
0
 
kretzschmarCommented:
well, if so, hamed,
then xperl should say it
:-)
0
 
XperlAuthor Commented:
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
 
h_mohsenianCommented:
it is an interesting way!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 16
  • 11
  • 5
  • +7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now