• Status: Solved
• Priority: Medium
• Security: Public
• Views: 333

# Changing numbers/non-numbers into numbers only

I need to change a sequence of numbers and non-numbers (groups of possibly 4 but not always) which are separated by dashes into another related sequence of only numbers (always length of 45 in groups of 5) but also separated by dashes.

EXAMPLE:

9244-A4AE-43F4-6598
into
92442-42243-24659-85948-20880-55838-24514-765879-23412

One way of doing this by using the first sequence:

Change non-numbers into 2's (9244-2422-4324-6598)
Remove dashes (9244242243246598)
Add random number/s at end to make 45 characters (9244242243246598 + 594820880558382451476587923412)
Insert a dash after every 5 characters (92442-42243-24659-85948-20880-55838-24514-765879-23412)

You may know a better way.  I need the code to do this.  I am using Delphi 6.  Your help would be greatly appreciated.  Thanks.
0
rincewind666
• 6
• 6
1 Solution

Commented:
So you want to change letter by number 2?
9244-A4AE-43F4-6598
9244-2422-4324-6598
0

Author Commented:
Yes or any other number
0

Commented:
try this function

function GetMyCode(str: string):string;
const number: string = '1234567890';
var I: integer;
rand: word;
begin
str := StringReplace(str, '-', '', []);
for I := 1 to Length(str) do
if Pos(str[I], Number) = 0 then
str := StringReplace(str, str[I], '2', []);
for I := 1 to 11 do
str := str + IntToStr(random(999));
for I := 1 to Length(str) do
if I mod 5 = 0 then
if result = '' then result := copy(str, 1, 5)
else result := result + '-' +copy(str, I - 5, 5) ;
end;
0

Commented:
This is update

function GetMyCode(str: string):string;
const number: string = '1234567890';
var I: integer;
rand: word;
begin
str := StringReplace(str, '-', '', [rfReplaceAll]);
for I := 1 to Length(str) do
if Pos(str[I], Number) = 0 then
str := StringReplace(str, str[I], '2', [rfReplaceAll]);
for I := 1 to 11 do str := str + IntToStr(random(999));
Form1.Edit1.Text := str;
for I := 1 to Length(str) do
if I mod 5 = 0 then
if result = '' then result := copy(str, 1, 5)
else result := result + '-' +copy(str, I - 4, 5) ;
end;
0

Author Commented:
Sorry but it doesn't quite work, although I think it's nearly there.  I get a number like
92442-24222-43242-65980-31860-20227-26703-18161-37142
92442-42243-24659-85948-20880-55838-24514-765879-23412
Note the first groups.

Also, sometimes I get something like
92442-24222-43242-65987-17306-16232-94652-46824-27848-11498
which is 10 groups instead of 9.

I appreciate your help with this.
0

Commented:
You found this result with last post (update)?
0

Author Commented:
Opps!  Missed that.  I will try again.
0

Commented:
Hi, this does exactly what you want:

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(FixString('9244-A4AE-43F4-6598'));
end;

function TForm1.FixString(s: String): String;
var
i: Integer;
begin
Result := StringReplace(s, '-', '', [rfReplaceAll]);
for i := 1 to Length(Result) do
if Result[i] > '9' then
Result[i] := '2';
while Length(Result) < 45 do
Result := Result + IntToStr(Random(9999));
Result := Copy(Result, 1, 45);
for i := 1 to Length(Result) + 5 do
if i mod 6 = 0 then
Insert('-', Result, i);
end;
0

Author Commented:
Excellent!  It works perfectly.  Many thanks for your help Mahdi78.  It is geatly appreciated.
0

Author Commented:
Excellent!  It works perfectly.  Many thanks for your help Mahdi78.  It is greatly appreciated.
0

Commented:
0

Commented:
Remove the following line
Form1.Edit1.Text := str;
0

Author Commented:
Yes.  I saw that.  Thanks.
0

## Featured Post

• 6
• 6
Tackle projects and never again get stuck behind a technical roadblock.