?
Solved

Counting Strings

Posted on 2003-03-27
4
Medium Priority
?
198 Views
Last Modified: 2010-05-18
Hey,
First of all I apologize for my English. It is not my first language.
I am new with Delphi.
I want to make a program that counts duplicated strings in a ListBox.
My program has a 2 component a button and a List box. If I push the button it must count how many strings or dabble and show it in a messageBox.
can somebody help me plz?

PS: I have tried with Array and For statement, but I didnt succeed.
0
Comment
Question by:kh420
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 8216998
If speed isn't a problem:

[Listbox called 'LB', Label called 'Label1']

procedure CountDoubles;
var
  lCount : integer;
  lIndex : integer;
  lPos   : integer;
begin
  lCount := 0;

  for lIndex := LB.Items.Count - 1 downto 1 do
  begin
    lPos := LB.Items.Find( LB.Items[lIndex] );
    if ( lPos >= 0 ) and ( lPos < lIndex ) then
    begin
      Inc( lCount );
    end;
  end;

  Label1.Caption = IntToStr(lCount) + ' duplicates';
end;

   
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 8217003
ooopss... 'Find' should be 'IndexOf'

and

after Label1.Caption

it should be

:=

instead of

=
0
 
LVL 9

Accepted Solution

by:
mocarts earned 150 total points
ID: 8217242
my version :)

procedure TForm1.Button1Click(Sender: TObject);
var
  st: TStringList;
  i, idx: integer;
  sMsg: string;
begin
  st := TStringList.Create;
  try
    for i := 0 to ListBox1.Items.Count -1 do begin
      idx := st.indexOf(ListBox1.Items[i]);
      if idx > -1 then
        st.Objects[idx] := pointer(integer(st.Objects[idx]) + 1)
      else
        st.AddObject(ListBox1.Items[i], pointer(0));
    end;
    for i := 0 to st.Count -1 do
      if integer(st.Objects[i]) > 0 then
      begin
        if sMsg <> '' then sMsg := sMsg + #13;
        sMsg := sMsg + '[' + inttostr(integer(st.Objects[i])+1) +'] ' + st[i];
      end;
    if sMsg <> '' then
      ShowMessage('Lines has duplicates:'+#13 + sMsg);
  finally
    st.Free;
  end;
end;

it's better of course to make this proc to some general by passing TStrings (ListBox.Items or Memo.Lines) to it and dealing with them.
wbr, mo.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8223071
my version :-))

unit checkfor_duplicates_u;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function checkfor_dub_strings(AStringList : TStrings) : Integer;
var
  sl : TStringList;
begin
  result := 0;  //pre-init
  sl := TStringList.Create;
  try
    sl.Sorted := true;
    sl.Duplicates := dupIgnore;
    sl.Assign(AStringList);
    result := AStringList.Count - sl.Count;
  finally
    sl.Free;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('There are '+inttostr(checkfor_dub_strings(ListBox1.Items))+' Duplicate Entries');
end;

end.

meikl ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question