|
-
Nov 8th, 2007, 11:29 AM
#1
Thread Starter
Fanatic Member
[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.
-
Nov 8th, 2007, 11:31 AM
#2
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
-
Nov 8th, 2007, 11:39 AM
#3
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!
-
Nov 8th, 2007, 11:43 AM
#4
Thread Starter
Fanatic Member
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.
-
Nov 8th, 2007, 11:45 AM
#5
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
-
Nov 8th, 2007, 12:53 PM
#6
Frenzied Member
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.
-
Nov 8th, 2007, 05:12 PM
#7
Hyperactive Member
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)
-
Nov 9th, 2007, 04:06 AM
#8
Re: [2005] Removing repeated chars in a string
Yes thats much better!
Good example.
Pino
-
Nov 9th, 2007, 11:10 AM
#9
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
-
Nov 9th, 2007, 11:41 AM
#10
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)
-
Nov 9th, 2007, 11:55 AM
#11
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|