Results 1 to 11 of 11

Thread: [2005] Removing repeated chars in a string

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    [2005] Removing repeated chars in a string

    Hi!!!

    I have a string like this "23723567345", how can I do so I turn it into this "234567". The string can change can be like "345234". But the numbers it contains only go from 1 to 9.
    In other words, how to remove the repeated chars in a string with only numbers from 1 to 9.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Removing repeated chars in a string

    Hey Lasering,

    When you say remove repeated chars, which way would you attack it from left to right or right to left?

    Ie does...

    12464849 = 124689 or 126849

    Thanks

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Removing repeated chars in a string

    Anyway here is one way of doing it, Hastable might be a little overkill but will show you how it could be done.

    Code:
            Dim myNumberStr As String = "568559722138735646"
            Dim usedNumbers As New Hashtable
            Dim endStr As String = ""
    
            For i As Integer = 1 To myNumberStr.Length - 1
                Dim curSel As String = myNumberStr.Substring(i, 1)
                If usedNumbers.Contains(curSel) = False Then
                    'Add it
                    usedNumbers.Add(curSel, curSel)
                    endStr &= curSel
                End If
            Next
    
            MessageBox.Show(endStr)
    Hope that helps!

  4. #4

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2005] Removing repeated chars in a string

    which way?? The way isn't important because if u remove the repeated chars from left to right or right to left u still would have the same number of numbers in the string. Yes the strings would be different but still with the same length and numbers just in a different order. I don't care about the order as long no repeated chars "stay" in the string.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Removing repeated chars in a string

    Ok then my code above will work. I wasn't sure if the order was important.

    Pino

  6. #6
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: [2005] Removing repeated chars in a string

    Here is a modified version of Pinos code (doesn't use a hashtable):


    Code:
    Dim myNumberStr As String = "568559722138735646"
    Dim endStr As String = String.Empty
    
    For Each curChar As Char In myNumberStr
        If Not endStr.Contains(curChar) Then
            endStr &= curChar
        End If
    Next
    
    MessageBox.Show(endStr)

    The result is "568972134"
    Last edited by nbrege; Nov 8th, 2007 at 12:58 PM.

  7. #7
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2005] Removing repeated chars in a string

    Another way of approaching it only using the string you start with.
    Code:
    Dim myNumberStr As String = "568559722138735646"
    
            For i As Integer = myNumberStr.Length - 1 To 1 Step -1
                If myNumberStr.IndexOf(myNumberStr.Substring(i, 1)) < i Then
                    myNumberStr = myNumberStr.Remove(i, 1)
                End If
            Next
    
            MessageBox.Show(myNumberStr)

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: [2005] Removing repeated chars in a string

    Yes thats much better!

    Good example.

    Pino

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] Removing repeated chars in a string

    For i As Integer = myNumberStr.Length - 1 To 1 Step -1

    wouldn't you want:
    For i As Integer = myNumberStr.Length - 1 To 0 Step -1

    since you're starting from Length -1 and things like SubString are zero based?

    I could be wrong on this one though
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] Removing repeated chars in a string

    Just for fun, here's my angle. I think it's a bit more efficient than those posted so far.

    Code:
    Dim input As String = "568559722138735646"
    Dim output As New StringBuilder(9)
    Dim numbersFree As Bool() = _
      {True, True, True, True, True, True, True, True, True}
    Dim sumOfUsed As Integer = 0
    
    For Each c As Char In input
      Dim digit As Integer = CInt(c) - 49
      
      If (numbersFree(digit)) Then
        numbersFree(digit) = False
        sumOfUsed += digit + 1
        output.Append(c)
      Else If (sumOfUsed = 45) Then
        Exit For
      End If
    Next
    
    Console.WriteLine(output)

  11. #11
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2005] Removing repeated chars in a string

    JuggaloBrotha, The loop checks for anything repeating at an index before the character index so there is no need to check on the last one (first character).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width