Recent

Author Topic: Replace Many Character to Number  (Read 1197 times)

e3xpic

  • New Member
  • *
  • Posts: 13
Replace Many Character to Number
« on: April 04, 2019, 04:53:23 pm »
How can i Replace Many Character in Edit1.text to Number in Edit2.text
for example
A to 1
B to 2
C to 3
D to 4

if text in Edit1.text='ABCD' will become Edit2.text='1234'
thank
im newbie in lazarus,tech me.sorry to my grammar translate :)

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Replace Many Character to Number
« Reply #1 on: April 04, 2019, 05:02:07 pm »
Code: Pascal  [Select][+][-]
  1. Edit2.Text := '';
  2. for j := 1 to Length(Edit1.Text) do
  3.    Edit2.Text := Edit2.Text + CHR(ORD(Edit1.Text[j])-16);
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: Replace Many Character to Number
« Reply #2 on: April 04, 2019, 05:03:46 pm »
There are many different ways to do it.

Usually, I will avoid hardcoding the constants. But if you don't mind, this code works as what you want:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   S: string;
  4. begin
  5.   S := Edit1.Text;
  6.   S := S.Replace('A', '1');
  7.   S := S.Replace('B', '2');
  8.   S := S.Replace('C', '3');
  9.   S := S.Replace('D', '4');
  10.   Edit2.Text := S;
  11. end;

e3xpic

  • New Member
  • *
  • Posts: 13
Re: Replace Many Character to Number
« Reply #3 on: April 04, 2019, 05:09:54 pm »
Perfect! Works like a charm. Thank you.  :)
im newbie in lazarus,tech me.sorry to my grammar translate :)

 

TinyPortal © 2005-2018