|
-
Oct 4th, 2010, 07:10 AM
#1
Thread Starter
Lively Member
[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
-
Oct 4th, 2010, 07:34 AM
#2
Re: String manipulation
Hi Onenew... Welcome to the forums...
What's your present code ? 
Whether this can be done?
Yes. It can be...! But first show us your effort. What have you coded so far ?
Last edited by akhileshbc; Oct 4th, 2010 at 07:48 AM.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 4th, 2010, 07:54 AM
#3
Re: String manipulation
You can call String.Split to split the numbers, then use a For loop with a Step clause to pick out the elements you want, then call String.Join to put them back together. Now that you know what to look for, you can search for examples and then put the information you find together to solve this particular problem.
-
Oct 4th, 2010, 08:02 AM
#4
Re: String manipulation
vb Code:
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strString1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
Dim strString2 As String = String.Empty
Dim intNum As Integer
Dim aa() As String
Dim i As Integer
Dim intCount As Integer
intNum = 8 '~~~ Pre-specified starting position (1 or 8)
aa = strString1.Split(","c) '~~~ Splitting
'~~~ When the starting position is other than 1
If intNum > 1 Then
'~~~ Resize the array and append the contents from the begining to the pre-specified starting point
intCount = aa.GetUpperBound(0)
ReDim Preserve aa(intCount + (intNum - 1)) '~~~ *Edit: I have cleared a small mistake in this line
For i = aa.GetLowerBound(0) To intNum - 2
aa(intCount + i + 1) = aa(i)
Next
End If
'~~~ Create the new string with each 3rd number
For i = intNum - 1 To aa.GetUpperBound(0) Step 3
strString2 = String.Concat(strString2, aa(i), ",")
Next
MessageBox.Show(strString2)
End Sub
....
Last edited by akhileshbc; Oct 4th, 2010 at 08:07 AM.
Reason: cleared a small mistake
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 4th, 2010, 08:31 AM
#5
Fanatic Member
Re: String manipulation
Umm I dunno if this will work as you want it to, but it seems to me that it should (note: I haven't actually tested this)
vb.net Code:
Dim String1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25" Dim firstSplit() As String = String1.Split(","c) Dim startingIndex As Integer = 5 Dim String2 As String = "" Do Until startingIndex > UBound(firstSplit) If String2 = "" Then String2 &= firstSplit(startingIndex) Else String2 &= "," & firstSplit(startingIndex) End If startingIndex += 3 Loop
I'm not sure on the effectiveness of this method but try it and see what you think.
-
Oct 4th, 2010, 10:55 AM
#6
Thread Starter
Lively Member
Re: String manipulation
Hi, akhileshbc,
Thanks for the great code. Code works mostly fine. It takes only a small change.
Originally, my string1 always ends with a comma character, as in this example:
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
This character is sometimes a cause for error. What a change in the code I need to do if I want to keep the comma character at the end of my string1?
Onenew
-
Oct 4th, 2010, 10:57 AM
#7
Re: String manipulation
 Originally Posted by Onenew
Hi, akhileshbc,
Thanks for the great code. Code works mostly fine. It takes only a small change.
Originally, my string1 always ends with a comma character, as in this example:
11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,
This character is sometimes a cause for error. What a change in the code I need to do if I want to keep the comma character at the end of my string1?
Onenew
Write the code to check if the String1's last character is a ",". If so, replace it with an empty string.
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 5th, 2010, 12:14 AM
#8
Fanatic Member
Re: [RESOLVED] String manipulation
vb.net Code:
If String1.EndsWith(","c) Then String1 = String1.SubString(0, String1.Length -1) End if
-
Oct 5th, 2010, 12:52 AM
#9
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.
Last edited by jmcilhinney; Oct 5th, 2010 at 12:55 AM.
-
Oct 5th, 2010, 02:26 AM
#10
Fanatic Member
Re: [RESOLVED] String manipulation
 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) 
I didn't know about the Step in a for statement so cheers for that
-
Oct 5th, 2010, 07:29 AM
#11
Re: [RESOLVED] String manipulation
 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 
Just kidding... 
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
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 5th, 2010, 07:31 AM
#12
Fanatic Member
Re: [RESOLVED] String manipulation
 Originally Posted by akhileshbc
I'm the first one who introduced the Step statement in code. So, you should thank me first
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
-
Oct 5th, 2010, 07:36 AM
#13
Re: [RESOLVED] String manipulation
 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 
Please note that, I won't accept amounts less than 20$
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 5th, 2010, 07:38 AM
#14
Fanatic Member
Re: [RESOLVED] String manipulation
Sorry mate, that'd clear out my bank account (unemployed 17-y/o ftw?)
-
Oct 5th, 2010, 07:40 AM
#15
Re: [RESOLVED] String manipulation
 Originally Posted by J-Deezy
Sorry mate, that'd clear out my bank account (unemployed 17-y/o ftw?)
I'm kidding..
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 5th, 2010, 08:45 AM
#16
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
-
Oct 5th, 2010, 11:37 AM
#17
Thread Starter
Lively Member
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
-
Oct 5th, 2010, 11:57 AM
#18
Fanatic Member
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
-
Oct 5th, 2010, 07:28 PM
#19
Re: [RESOLVED] String manipulation
 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?
-
Oct 5th, 2010, 08:05 PM
#20
Re: [RESOLVED] String manipulation
@jmc - Winner, winner, chicken dinner.
-
Oct 5th, 2010, 10:06 PM
#21
Re: [RESOLVED] String manipulation
 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? 
Didn't you see this word?
I'm the first one who introduced the Step statement in code.

So, we can share it. 30 for you and 70 for me
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Oct 5th, 2010, 10:22 PM
#22
Thread Starter
Lively Member
Re: [RESOLVED] String manipulation
Hi, J-Deezy,
Thanks for great code.
Onenew
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
|