Remove penultimate character from a string

-1

I have an application that clicks on labels a edit gets its caption as follows:

Edit1.Text := Edit1.Text + TLabel(Sender).caption+ ' ';

After that I click ok, and this information changes to memo . But the problem now appears when clicking ok this code sends the data to memo :

Memo1.Lines.add('    '+'if'+' '+ cond1+':');

The memo gets the following value for example:

    if dose > 0 :

You may notice that after 0 there is a space, I would like to remove it so it looks like this:

    if dose > 0:
    
asked by anonymous 10.12.2015 / 14:44

2 answers

0

try using as follows

var tamanho : integer;
begin
  if trim(edit1.text) <> '' then  // valida se o edit esta preenchido
  begin
     tamanho := Length(edit1.Text);   // pega o tamanho da variavel
     if copy(edit1.Text,tamanho-1,tamanho) = ' ' then  // verifica se é espaço nulo
        Memo1.Lines.add(deletechar(' ',edit1.text));
  end;
end;

function DeleteChar(Ch: Char; S: string): string;   // deleta o caracter da variavel na possição informada antes...
var Posicao: integer;
begin
   Posicao := tamanho-1;
   Delete(S,Posicao, 1);
end;
    
30.12.2015 / 15:20
0

There are several ways to do this is one of them, just create the following function:

function TfrmMain.GetResult(val1: String): String;
var val2, val3: String;
begin
  val2 := copy(val1, 1,  Length(val1) - 2);
  val3 := copy(val1, Length(val1),  1);

  Result := val2 + val3;
end;

And it will be necessary in the event of a button eg call the function:

procedure TfrmMain.BtnGetResultClick(Sender: TObject);
var val1: String;
begin
  val1 := 'if dose > 0 :';
  Memo1.Lines.Add(GetResult(val1));
end;
    
04.11.2016 / 11:24