rincewind666
asked on
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-20 880-55838- 24514-7658 79-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 + 59482088055838245147658792 3412)
Insert a dash after every 5 characters (92442-42243-24659-85948-2 0880-55838 -24514-765 879-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.
EXAMPLE:
9244-A4AE-43F4-6598
into
92442-42243-24659-85948-20
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 + 59482088055838245147658792
Insert a dash after every 5 characters (92442-42243-24659-85948-2
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.
ASKER
Yes or any other number
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;
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry but it doesn't quite work, although I think it's nearly there. I get a number like
92442-24222-43242-65980-31 860-20227- 26703-1816 1-37142
instead of something like
92442-42243-24659-85948-20 880-55838- 24514-7658 79-23412
Note the first groups.
Also, sometimes I get something like
92442-24222-43242-65987-17 306-16232- 94652-4682 4-27848-11 498
which is 10 groups instead of 9.
I appreciate your help with this.
92442-24222-43242-65980-31
instead of something like
92442-42243-24659-85948-20
Note the first groups.
Also, sometimes I get something like
92442-24222-43242-65987-17
which is 10 groups instead of 9.
I appreciate your help with this.
You found this result with last post (update)?
ASKER
Opps! Missed that. I will try again.
Hi, this does exactly what you want:
procedure TForm1.Button1Click(Sender : TObject);
begin
ShowMessage(FixString('924 4-A4AE-43F 4-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;
procedure TForm1.Button1Click(Sender
begin
ShowMessage(FixString('924
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;
ASKER
Excellent! It works perfectly. Many thanks for your help Mahdi78. It is geatly appreciated.
ASKER
Excellent! It works perfectly. Many thanks for your help Mahdi78. It is greatly appreciated.
You are welcome, i am glad to help you ;)
Remove the following line
Form1.Edit1.Text := str;
Form1.Edit1.Text := str;
ASKER
Yes. I saw that. Thanks.
9244-A4AE-43F4-6598
9244-2422-4324-6598