Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
From string f.e. "AA--BB-C----B--AAADEE" I need to get output "2A2BCB3AD2E"

The code below does that well, but I was wondering if there is a simpler way to achieve this without too much IF conditions?

Thanks

Dim tz As String = ""
Dim pz As String = ""
Dim zn As Integer = 1
Dim code As String = TextBox1.Text
Dim print As String = Nothing

For i = 1 To Len(code)
tz = Mid(code, i, 1)
If tz <> "-" Then
If pz = tz Then
zn = zn + 1
Else
If pz <> "" Then
If zn > 1 Then
print = print & zn & pz
zn = 1
Else
print = print & pz
End If
End If
End If
pz = tz
End If
Next
If zn > 1 Then
print = print & zn & pz
tz = 1
Else
print = print & pz
End If
TextBox2.Text = print
Posted
Comments
E.F. Nijboer 5-Aug-15 5:42am    
Looks like a school asignment...
Maciej Los 5-Aug-15 6:03am    
I would recommend to read Unhappy Inquirer or Is the Abuse the Main Purpose of Programming? post on Sergey's Alexadrovich Kryukov profile page.

1 solution

Well, to give you some tips... (although I don't see any relation between the dashes and the output)


You could simply use standard string functions to split the string on each dash and leave out the empty strings. Then you can simply loop through the array and count all the same characters and output these.


Look into the standard string functions to check out what they can do to help you out.


Good luck!

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900