GS1 company prefix

  Kiến thức lập trình

I am trying to get the company prefix from barcodes using dart:

String extractCompanyPrefix(String gtin) {
  // Ensure the GTIN is a string to handle leading zeros and perform string operations
  String gtinStr = gtin.padLeft(14, '0');

  String prefix = gtinStr.substring(0, 8);
  // remove leading zeros
  prefix = prefix.replaceFirst(RegExp(r'^0+'), '');
  return prefix;

void main(List<String> arguments) {
  if (arguments.isEmpty) {
    print('Please provide a barcode.');

  String barcode = arguments.first;
  String companyPrefix = extractCompanyPrefix(barcode);
  print('Company prefix: $companyPrefix');

❯ dart run dart/company_prefix_checker.dart 8710398527905
Company prefix: 8710398
❯ dart run dart/company_prefix_checker.dart 40111445
Company prefix: 40

Is that the correct way and results?

Correct company prefix

New contributor

W. Gad is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.