Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   IBAN String Formatieren (https://www.delphipraxis.net/209872-iban-string-formatieren.html)

josef-b 2. Feb 2022 16:54

Delphi-Version: 10 Seattle

IBAN String Formatieren
 
Ich möchte gerne IBAN (Bankkontonummern) formatieren.

ich habe:

einen String wie 'DE12123412341234123412'

und ich möchte: 'DE 12 1234 1234 1234 1234 12'

Das ist bestimmt ganz einfach, aber ich komme nicht drauf.

Delphi-Quellcode:

evtl.

format('%S.....?

Uwe Raabe 2. Feb 2022 17:20

AW: IBAN String Formatieren
 
Delphi-Quellcode:
uses
  System.MaskUtils;

var
  formattedIBAN: string;
begin
  formattedIBAN := FormatMaskText('LL 00 0000 0000 0000 0000 00;0; ', 'DE12123412341234123412');
end;

Sherlock 2. Feb 2022 17:21

AW: IBAN String Formatieren
 
Schau Dir mal VCL.TMaskEdit an. Ich persönlich finde das ziemlich unschön, aber wenn Du es wirklich brauchst...

Edith sagt: Uwe war schneller...

Sherlock

josef-b 2. Feb 2022 17:56

AW: IBAN String Formatieren
 
Vielen Dank Ihr zwei..

BerndS 2. Feb 2022 18:06

AW: IBAN String Formatieren
 
Formatiert man das nicht immer in Blöcken zu vier Zeichen?
DE12 3456 7890 1234 5678 90

Das habe ich mal eine einfache Funktion gepackt.
Delphi-Quellcode:
const
  cThinNoBreakSpace = #8239; // siehe https://de.wikipedia.org/wiki/Schmales_Leerzeichen

function FormatIBAN(AIBAN: string; AThinNonBreakSpace: Boolean = False): string;
var
  I: Byte;
  C: Char;
begin
  Result := AIBAN;
  if AThinNonBreakSpace then
    C := cThinNoBreakSpace
  else
    C := #32;
  for I := 1 to 5 do
    Insert(C, Result, I * 5);
end;
Durch das schmale Leerzeichen wird wenig Platz verschwendet und es wird nicht umgebrochen.

himitsu 2. Feb 2022 18:35

AW: IBAN String Formatieren
 
Da ich sonst nie zum Regexen komm' :cry:, aber Einzeiler so ma ... https://regex101.com/r/nnuwfo/1

Delphi-Quellcode:
uses System.RegularExpressions;

var IBAN := 'DE12345678901234567890';
IBAN := TRegEx.Replace(IBAN, '(\w{4})', '$1 ');
ShowMessage(IBAN);
Delphi-Quellcode:
var IBAN := 'DE1234 5678901 234   567890';
IBAN := TRegEx.Replace(IBAN.Replace(' ', ''), '(\w{4})', '$1 ');
ShowMessage(IBAN);
oder statt
Delphi-Quellcode:
\w
ein
Delphi-Quellcode:
[A-Z0-9]
,
Delphi-Quellcode:
[a-zA-Z0-9]
oder
Delphi-Quellcode:
[[:alnum:]]


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:17 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz