Remove penultimate character from a string

Asked

Viewed 2,042 times

0

I have an application that by clicking on labels one edit receives his caption as follows:

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

After that I click ok, and this information passes to a memo. However the problem appears now by clicking ok that code sends the data to memo:

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

The memo receives the following value for example:

    if dose > 0 :

You can notice that after 0 there is a space, I would like to remove it so that it is as follows:

    if dose > 0:
  • 1

    Try using Trimleft, Trimright, or Trim to remove the blanks.

  • do not want to remove all whitespace just what I indicated in the question

  • 1

    See if it can help you adapt your question. http://www.devmedia.com.br/forum/deletar-ultimo-caracter-de-umastring/158822

  • If your if had something like if dose > 01: he would have to remove the 1?

  • Trimleft or Trimright does not remove all blank spaces from the string.

  • 1

    Thanks @Rboschini the Trimright function solved my problem.

Show 1 more comment

2 answers

0


try to use 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;

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 for example to call the Function:

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

Browser other questions tagged

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