Hello,

who can tell me how to format a string in Delphi?
I must format 09-120-123456 into: 091200123456
it is, remove the dashes and insert additional 0 in the middle to make a 13 character long string.

I searched Internet, but I can't find anything understandable...
like:
[Format(<qReportIzpis."acDoc">,the result???)]

this is for a report..

thank youuu

Well, you can format a number into string, or a date into string, but formating a string... it is just not the word, it is more a transformation... well, who minds about the name!

In your case, you just want to change all '-' with '', so you onlky need it:

MyText:= StringReplace(MyText, '-', '', [rfReplaceAll]);

Difficult. But you may be able to adapt the below.

3 Function and 1 definition

TYPE StringArray = ARRAY OF STRING;

to test would be 1st bit of code

procedure TMain.Button1Click(Sender: TObject);
var
  a : StringArray;
begin
  IF E1.TEXt = ''  THEN Exit;
  A := parsestring(e1.text,'-');
  E1.text := A[0] + A[1] + '0' + A[2];
End;

FUNCTION TokenCount(S : STRING ; Token : Char) : Integer;

VAR
  I : Integer;

Begin
	Result := 0;
  FOR I := 1 TO Length(S) DO
  	IF S[i] = Token THEN Inc(Result);
End;


FUNCTION ParseString(S : STRING; Delimiter : Char) : StringArray;

VAR
  RString 	 : StringArray;
  TCount,
  i         : INTEGER;

Begin
   TCount := TokenCount(S,Delimiter) + 1;
   SetLength(RString,TCount);
   IF Length(Rstring) > 0
   THEN
   FOR I := 0 TO TCount-1 DO
   	RString[i] := GetSubString(S,Delimiter);
   RESULT := RString;
End;

FUNCTION GetSubstring(VAR S : STRING;Ch : Char) : STRING;

VAR Sub : STRING;

Begin
	RESULT := '';
  WHILE  s[1] = ch DO Delete(s,1,1);
  Sub := '';
  WHILE ((Length(S) > 0) AND (s[1] <> Ch)) DO
  Begin
  	Sub := Sub + S[1];
     Delete(s,1,1);
  End;
  RESULT :=  Sub;
End;

Enjoy

P.

Hello,

who can tell me how to format a string in Delphi?
I must format 09-120-123456 into: 091200123456
it is, remove the dashes and insert additional 0 in the middle to make a 13 character long string.

I searched Internet, but I can't find anything understandable...
like:
[Format(<qReportIzpis."acDoc">,the result???)]

this is for a report..

thank youuu

Hi

procedure TForm1.Button1Click(Sender: TObject);
var
 astring:string;
begin
  aString:='09-120-123456';

  astring[7]:='0';
  delete(astring,3,1);

  Showmessage(astring);

end;

What a dificult way to do such a simple thing!

quaifp1:
You avoid using Pos('-', txt) and instead construct a whole piece of code to do it! And then repeat the same with StringReplace... all your code is done with my one-line-code!

cao:
Your code doesn't do anything, the string could be different each time, and changing the first '-' with a '0' is not the point anyway.

Edited 7 Years Ago by BitFarmer: n/a

BitFarmer:
Please test the code first before making assumptions that it does not work

The code does exactly what is required - of course the string would be different each time thats why I have an init section. Read the code again and you will see that the 7th char '-' is replaced with a '0' and the 3rd char '-' is deleted as required - which is exactly the point!

Maracaibolagi said "remove the dashes and insert additional 0 in the middle to make a 13 character long string. "

cao

Guys, thank you very much for your help.
I wanted it to be a one-line code....
here is what I came up with:

(Copy(<qReportIzpis."acDoc">,1,2) + Copy(<qReportIzpis."acDoc">,4,3) + '00'+Copy(<qReportIzpis."acDoc">,8,6) )

It works for me.

cao: Yes, your code solve the exact example showed, but you assume the - will always be on the same positions... it is not said anywhere, so if next string suplied is, for instance, 0-9-120-123456 Have you tested your code with that one? It will mesh up averything.

maracaibolago: Reading your question again, you don't give all the details to be able to know your problem:

All the string you are going to format are going to be in that EXACTLY format, or can they come with more/less '-' or bigger number in between?

Why you add zero in a particular '-' and not in the other one?

What if the string doesn't has any '-'? Where do you add zeros?

And more disgusting: Why the code you finaslly supplie DOESN'T format the example you gave like in your example? (the zeros are added in diferent positions) Why your example only has 12 chars? Why in your example you added a zero in exactly that position ?

Anyway, I think this question was imposible to solve with the info and example given, so all the code we have sent, is useless...

Actually...I think quaifp1 code is probably the best...It does what it needs to do...It assumes that we basically have 3 strings that need to be concatenated together with rules...Only need to add a Pad Zero function to make it work for any length chars...

Though if I was going to make this production...I would probably check to make sure each Array index was not longer than it was suppose to be...

unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  LenA0 = 2;
  LenA1 = 3;
  LenA2 = 7;

type
  TForm6 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  StringArray = ARRAY OF STRING;

var
  Form6: TForm6;

implementation

{$R *.dfm}
Uses
  StrUtils;


FUNCTION TokenCount(S : STRING ; Token : Char) : Integer;
VAR
  I : Integer;
  Begin
    Result := 0;
    FOR I := 1 TO Length(S) DO
    IF S[i] = Token THEN Inc(Result);
End;

FUNCTION GetSubstring(VAR S : STRING;Ch : Char) : STRING;
VAR Sub : STRING;
Begin
  RESULT := '';
  WHILE s[1] = ch DO Delete(s,1,1);
  Sub := '';
  WHILE ((Length(S) > 0) AND (s[1] <> Ch)) DO
  Begin
    Sub := Sub + S[1];
    Delete(s,1,1);
  End;
  RESULT := Sub;
End;


FUNCTION ParseString(S : STRING; Delimiter : Char) : StringArray;
VAR
  RString : StringArray;
  TCount,
  i : INTEGER;
Begin
  TCount := TokenCount(S,Delimiter) + 1;
  SetLength(RString,TCount);
  IF Length(Rstring) > 0 THEN
    FOR I := 0 TO TCount-1 DO
      RString[i] := GetSubString(S,Delimiter);
    RESULT := RString;
End;


function LeftPad(value: string; length:integer=7; pad:char='0'): string; overload;
begin
  result := RightStr(StringOfChar(pad,length) + value, length );
end;


procedure TForm6.Button1Click(Sender: TObject);
var
a : StringArray;
begin
  IF Edit1.TEXt = '' THEN Exit;
  A := parsestring(Edit1.text,'-');
  if (Length(a[0]) > LenA0) or (Length(a[1]) > LenA1) or (Length(a[2]) > LenA2) then
    Edit1.Text := 'Invalid value, Length is not in expected range.'
  else
    Edit1.text := LeftPad(A[0], LenA0) + LeftPad(A[1], LenA1) + LeftPad(A[2],LenA2);
end;

end.

Edited 7 Years Ago by House_of_Dexter: n/a

This question has already been answered. Start a new discussion instead.