Get white space from a string

Asked

Viewed 1,012 times

1

I’m looking for something like this:

I have a string like this:

      texto
           texto
  texto
                 texto

type, with spaces before the text, and I need to take this space, and save in a variable, this using Delphi 7. Except that the text is not always text, it can be 123, might be another word, but I need to save the size of the space, 'cause I’m gonna need it later.

Any suggestions? I tried pos/copy, but without success.

1 answer

2


My Pascal is long forgotten but I think he can do something like that:

Var S : String;

S:=StringOfChar(' ',Length(ASuaVariavel) - Length(TrimLeft(AsuaVariavel));  

Edit after your comment

In the question, you say that strings have spaces at the beginning, in the comment refers that they are Tabs, then the code needs to be changed.

var
  suaString : string;
  TAB : char;
  tabCount : Integer;
  stringOfTabs : string;
begin

  TAB := #9;
  suaString:= TAB + TAB + TAB + 'Texto';
  tabCount := Length(suaString) - Length(TrimLeft(suaString));
  stringOfTabs := StringOfChar(TAB,tabCount);

  writeln('A sua string: ', suaString);
  writeln('Numero de Tabs: ', tabCount);
  writeln(stringOfTabs + 'Tabs aplicados a outra string');

end.

See on Ideone

  • Thank you. I’ll try my job tomorrow, and I’ll get back to you. From what I read, the Stringofchar function is used to take small pieces of a string and convert it into char, something like that. Detail that these spaces are tabs, so they can have 1, 2 or more tabs, regardless of that, I have to know how many are, because I will need to concatenate this with another string later.

  • ramaral, I did it here and it worked. Instead of using Stringofchar, I used the Dupestring function, it was good. But I used spaces, instead of tab, because it was getting too out of identation. Ah, I forgot to mention, this is for an sql code finder that we are doing to embed within our ERP. Thank you very much for your attention :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.