[RESOLVED] String manipulation
(Visual Basic 2008 Express edition)
I have string1 that may contain from 6 to 180 integers and comma characters, something like this:
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
The number of integers is always divisible by the number 3.
I need to make a new string2, containing every third integer from string1 but starting from the pre-specified integer (in TextBox1), at a special procedure, as follows:
If TextBox1.Text = 1 then string2 (for the example above) should look like this:
String1 = 11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
String2= 11,14,17,20,23
But, If the TextBox1.Text = 8 then string2 (for the example above) should look like this:
String1=11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
String2= 18,21,24,12,15,
Whether this can be done?
I hope to get a help.
Thanks in advance.
Onenew
Re: [RESOLVED] String manipulation
vb.net Code:
If String1.EndsWith(","c) Then
String1 = String1.SubString(0, String1.Length -1)
End if
:wave:
Re: [RESOLVED] String manipulation
I was hoping that the OP would be able to do some thinking for themselves but, as we're taking that responsibility away from them:
vb.net Code:
Private Function GetSubset(ByVal numbers As String, ByVal startIndex As Integer) As String
Dim superset As String() = numbers.Split(New Char() {","c}, _
StringSplitOptions.RemoveEmptyEntries)
Dim subset As New List(Of String)
For index = startIndex To superset.GetUpperBound(0) Step 3
subset.Add(superset(index))
Next
Return String.Join(",", subset.ToArray())
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
Dim str2 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
MessageBox.Show(Me.GetSubset(str1, 1 - 1))
MessageBox.Show(Me.GetSubset(str2, 8 - 1))
End Sub
EDIT: Actually, now that I look at the examples provided in the first post, this code won;t do what they indicate is required, although it will do what's actually described in the words of the post.
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
jmcilhinney
I was hoping that the OP would be able to do some thinking for themselves but, as we're taking that responsibility away from them:
vb.net Code:
Private Function GetSubset(ByVal numbers As String, ByVal startIndex As Integer) As String
Dim superset As String() = numbers.Split(New Char() {","c}, _
StringSplitOptions.RemoveEmptyEntries)
Dim subset As New List(Of String)
For index = startIndex To superset.GetUpperBound(0) Step 3
subset.Add(superset(index))
Next
Return String.Join(",", subset.ToArray())
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
Dim str2 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
MessageBox.Show(Me.GetSubset(str1, 1 - 1))
MessageBox.Show(Me.GetSubset(str2, 8 - 1))
End Sub
EDIT: Actually, now that I look at the examples provided in the first post, this code won;t do what they indicate is required, although it will do what's actually described in the words of the post.
I like your solution, I should have though of building a list(of string) :mad:
I didn't know about the Step in a for statement so cheers for that ;)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
J-Deezy
I didn't know about the Step in a for statement so cheers for that ;)
I'm the first one who introduced the Step statement in code. So, you should thank me first :cool:
Just kidding... ;)
Quote:
I was hoping that the OP would be able to do some thinking for themselves but, as we're taking that responsibility away from them
I was waiting for the OP to post the code that he had done. But he didn't posted anything. That's why I posted my code.
I won't do that again :)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
akhileshbc
I'm the first one who introduced the Step statement in code. So, you should thank me first :cool:
Just kidding... ;)
Hehe I was making a general statement that's why I separated it from my comment at jmcilhinney ;), but if it makes you happy:
Thank you akhileshbc for introducing the Step statement to me first in this thread :)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
J-Deezy
Hehe I was making a general statement that's why I separated it from my comment at jmcilhinney ;), but if it makes you happy:
Thank you akhileshbc for introducing the Step statement to me first in this thread :)
No..No.. I don't accept thanks as "words". You can send me PayPal money as a sign of appreciation and that will make me happy :lol:
Please note that, I won't accept amounts less than 20$ :afrog:
Re: [RESOLVED] String manipulation
Sorry mate, that'd clear out my bank account (unemployed 17-y/o ftw?)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
J-Deezy
Sorry mate, that'd clear out my bank account (unemployed 17-y/o ftw?)
I'm kidding..:)
Re: [RESOLVED] String manipulation
jmc's code but using StringBuilder
Code:
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim str1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31"
Debug.WriteLine(GetSubset(str1, 0, 0)) 'test error
Debug.WriteLine(GetSubset(str1, 0)) '11 is entry 0
Debug.WriteLine(GetSubset(str1, 2, 2)) '13 is entry 2
End Sub
Private Function GetSubset(ByVal theCSVstring As String, _
ByVal startIndex As Integer, _
Optional ByVal EveryX As Integer = 3) As String
'error checks
'If EveryX <= 0 Then Throw New ArgumentOutOfRangeException("EveryX", "must be greater than 0")
' or
If EveryX <= 0 Then Return ""
Dim theSet As String() = theCSVstring.Split(New Char() {","c}, _
StringSplitOptions.RemoveEmptyEntries)
'use StringBuilder to minimize creation of strings
Dim subset As New System.Text.StringBuilder
'startIndex is zero based
For index As Integer = startIndex To theSet.Length - 1 Step EveryX
subset.Append(theSet(index).Trim)
subset.Append(",")
Next
subset.Length = subset.Length - 1 'get rid of trailing comma
Return (subset.ToString) 'return subset
End Function
Re: [RESOLVED] String manipulation
Hi, jmcilhinney, J-Deezy, dbasnett,
I'd like to try some other code, which you gave, but that's not what I wanted.
A little more explanation.
- the number of integers in a new string2 is one third of string1.
- a new string can start from any integer in string1, taking every third integer to the end of string1 and then from the beginning of string1 to the integers from which we started.
I hope that I managed to explain, despite my poor english.
Onenew
Re: [RESOLVED] String manipulation
Ohhh I understand now, I never read your second example. Do you mean that if from the starting index to the end of the string contains less than one third of the total numbers, start again from the beginning of string1? If so:
vb.net Code:
Dim String1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
If String1.ToLower.EndsWith(",") Then
String1 = String1.Substring(0, String1.Length - 1)
End If
Dim str() As String = String1.Split(","c)
Dim totalElements As Integer = str.Count / 3
Dim StartingIndex As Integer = CInt(TextBox1.Text) - 1
Dim theList As New List(Of String)
Dim currentInt As Integer = StartingIndex
Do
If theList.Count = totalElements Then Exit Do
If currentInt > str.Count Then currentInt -= str.Count
theList.Add(str(currentInt))
currentInt += 3
Loop
Dim string2 As String = String.Join(",", theList.ToArray)
MsgBox(string2)
It's pretty messy, I haven't got time to go through and clean it up so that's now your problem :)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
akhileshbc
I'm the first one who introduced the Step statement in code.
Yes, but I mentioned it first in post #3, so all the thanks and PayPal money and chocolate is rightfully mine. Mine, I tell you! Mwah hah haH! Oops. Did I say that out loud? :blush:
Re: [RESOLVED] String manipulation
@jmc - Winner, winner, chicken dinner. :)
Re: [RESOLVED] String manipulation
Quote:
Originally Posted by
jmcilhinney
Yes, but I mentioned it first in post #3, so all the thanks and PayPal money and chocolate is rightfully mine. Mine, I tell you! Mwah hah haH! Oops. Did I say that out loud? :blush:
Didn't you see this word?
Quote:
I'm the first one who introduced the Step statement in code.
:p
So, we can share it. 30 for you and 70 for me :D
Re: [RESOLVED] String manipulation
Hi, J-Deezy,
Thanks for great code.
Onenew