שתף באמצעות


Checking for duplicate values in Strings

Question

Wednesday, August 5, 2009 2:45 AM

Hi all,

i am new to VB.NET and i was wondering if anyone here knows how to check for duplicate values in strings?

Any help would be greatly appreciated.

Regards,
Charles

All replies (11)

Wednesday, August 5, 2009 5:45 AM ✅Answered | 1 vote

use this query to check the duplicate phone number while editing or inserting a row..

SELECT

 

COUNT(*) FROM Customer WHERE PNum = <phone number> AND CUSTID != <CUSTID>

while inserting replace the  <custid> with -1 and while editing repalce it with the editing customer id.

check the return value . if it is greater than one then the phone number exists..

:: Learning .NET ::


Wednesday, August 5, 2009 4:37 AM

What kind of value are you checking for in the string (Char or Word)?. You can break the string into array of string and use IndexOf each string in array.

kaymaf

I hope this helps, if that is what you want, just mark it as answer so that we can move on


Wednesday, August 5, 2009 4:39 AM

Can you give a little more info?

what duplicate values? Characters, words???

where are the strings? In a textfile, ListBox???


Wednesday, August 5, 2009 5:15 AM | 1 vote

This is a bit of a hack, and looks expensive to execute, but if you wanted to filter out duplicates from a delimited list it would work:

Imports System.Text

Module Module1

    Sub Main()
        Dim Input As String = "a,b,a,b,b,c,d,e,b,a"
        Dim Raw As New List(Of String)(Input.Split(","))
        Dim Filtered As New List(Of String)
        Dim SB As New StringBuilder()
        Dim Result As String

        For Each s As String In Raw
            If Not Filtered.Contains(s) Then
                Filtered.Add(s)
                SB.Append(s & ",")
            End If
        Next s

        Result = SB.ToString(0, SB.Length - 1)
        Console.WriteLine(Result)
    End Sub

End Module

Similarly, you could use the Filtered.Contains(s) test to identify which values are duplicates. There is probably a better way to do this, maybe with LINQ. If I figure that out tonight, I'll post it.


Wednesday, August 5, 2009 5:28 AM

Yes, the LINQ Enumerable.Distinct method will filter duplicates out of a sequence.

Imports System.Text
Imports System.Linq

Module Module1

    Sub Main()
        Dim Input As String = "a,b,a,b,b,c,d,e,b,a"
        Dim SB As New StringBuilder()
        Dim Result As String = ""

        For Each s As String In Input.Split(",").Distinct
            SB.Append(s & ",")
        Next s

        Result = SB.ToString(0, SB.Length - 1)
        Console.WriteLine(Result)
    End Sub

End Module

Wednesday, August 5, 2009 5:38 AM

Sorry guys, seems like i haven't provide u guys with enough information of what i am working on, I am working on a project affiliated to customer's database and i am required to insert, update, delete the data through a window's form program. The database i am using is MS SQL server 2005 and the columns in it includes, CustName, CustID, BlkName, UnitNum, PNum. I need to check for duplicate values for the field PNum whereby there shouldnt be any duplicate phone numbers inside the database. I am also using datagridview to display the customer's data.

Thank you all for your kind responses,
Charles


Wednesday, August 5, 2009 5:58 AM

Thanks for the suggestion Prasant, but do you mind telling me how am i suppose to write it inside my program?


Wednesday, August 5, 2009 6:06 AM | 1 vote

Like prasant showed is this not a VB problem, however a kind of SQL server problem.

http://social.msdn.microsoft.com/Forums/en-US/transactsql/threads

Although you could be doing this by using Linq To SQL

http://msdn.microsoft.com/en-us/bb688088.aspxSuccess
Cor


Wednesday, August 5, 2009 6:12 AM | 1 vote

No problem, Charles. It was fun trying a bit of LINQ anyway.

I think Prasant Swain is basically correct. Use a SELECT COUNT query to check for duplicate phone numbers before doing inserts or updates. 

Before doing an INSERT, your SELECT COUNT would be like this:

    SELECT COUNT(*) FROM Customer WHERE PNum = <phone number>

... where <phone number> is the value for which you want no duplicates.

Before doing an UPDATE, your SELECT COUNT would be like this:

    SELECT COUNT(*) FROM Customer WHERE PNum = <phone number> AND CustID != <CustID>

... where CustID is the customer ID on which you're about to do the update.

In both cases, if the number of records returned by the SELECT COUNT query is greater than ZERO, there is a duplicate and you should not execute the insert or update.


Wednesday, August 5, 2009 6:30 AM

Atctually, Matt, as a matter of fact, i understand Prasant's SQL statement, the only issue is that because i am new to VB.NET i have no idea how am i gonna apply this sql statement into my codings and also that i am checking the whole database for any duplicate phone number, not a phone number i cannot duplicate.


Wednesday, August 5, 2009 1:24 PM | 1 vote

So you know SQL? If your intention is to find existing duplicates:

    SELECT PNum, COUNT(*) FROM Customer GROUP BY PNum HAVING COUNT(*) > 1

Your other question seems to be how to connect to databases and execute queries from VB.NET. Beth Massi's blog links to a lot of good material on VB, including Code Samples, and her own "How Do I" videos which show how to work with databases in VB.

http://blogs.msdn.com/bethmassi/default.aspx