Find And Replace Duplicate Word

skmishra211

New member
Joined
Nov 23, 2009
Messages
1
Programming Experience
3-5
Dear All
I have a tab delimitted .txt file, I am reading successfully. Reading first line and split into array. Through the loop I am checking the duplicate column name.

Now I want, if duplicated word found, how can I replace the second one. Suppose my file contains
VB.NET:
Cell    Site     Cell ID      Cell ID   Site Detail
1        A         1             A1       ABC
2        B         2             B2        XYZ

I want to replce second Cell ID to Cell ID_2.

VB.NET:
Cell    Site     Cell ID      Cell ID_2   Site Detail
1        A         1             A1       ABC
2        B         2             B2        XYZ

How can I achieve this. Please help
Regards
Sanjay
 
Heres a Suggestion?

If you know what the phrase is going to be you can do something like this?

    Sub Main()
        Dim searchIn As String = "Cell Site Cell ID Cell ID Site Detail Cell ID Mark Cell ID Cell ID And"
        Console.WriteLine(searchIn)
        Console.WriteLine(renameDuplicatePhrases("Cell ID", searchIn))

        'Outputs:
        'Cell Site Cell ID Cell ID Site Detail Cell ID Mark Cell ID Cell ID And
        'Cell Site Cell ID Cell ID_2 Site Detail Cell ID_3 Mark Cell ID_4 Cell ID_5 And

        Console.ReadLine()
    End Sub

    Function renameDuplicatePhrases(ByVal phrase As String, ByVal containedIn As String) As String
        Dim renamedString As String = ""

        If containedIn.Contains(phrase) Then
            'remove the first one
            Dim searchIn As String = containedIn
            Dim startLocation As Integer = searchIn.IndexOf(phrase)
            Dim length As Integer = phrase.Length
            Dim endLocation As Integer = startLocation + length

            renamedString = searchIn.Substring(0, endLocation)
            searchIn = searchIn.Substring(endLocation)

            Dim phraseCounter As Integer = 2
            'start counter and renaming
            While searchIn.Contains(phrase)
                Dim nextStartLocation As Integer = searchIn.IndexOf(phrase)
                Dim nextEndLocation As Integer = nextStartLocation + length

                renamedString &= searchIn.Substring(0, nextEndLocation).Replace(phrase, String.Format("{0}_{1}", phrase, phraseCounter))
                searchIn = searchIn.Substring(nextEndLocation)
                phraseCounter += 1
            End While

            If searchIn <> "" Then
                renamedString &= searchIn
            End If
        End If

        Return renamedString
    End Function


of course you can rewrite it to be a bit more efficient, but you get the idea. If you dont know what the duplicate word is your going to have to write something that searches through the line to find the duplicate words. I'm not sure what the most efficient way to do this would be but you can create a loop and go through the words in the line progressively to find duplicates and then use a method like i posted above to rename it.
 
Last edited:
Back
Top