Results 1 to 22 of 22

Thread: [RESOLVED] String manipulation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    83

    Resolved [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

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    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,...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: String manipulation

    vb Code:
    1. Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim strString1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    3.         Dim strString2 As String = String.Empty
    4.  
    5.         Dim intNum As Integer
    6.         Dim aa() As String
    7.         Dim i As Integer
    8.         Dim intCount As Integer
    9.  
    10.         intNum = 8  '~~~ Pre-specified starting position (1 or 8)
    11.         aa = strString1.Split(","c) '~~~ Splitting
    12.  
    13.         '~~~ When the starting position is other than 1
    14.         If intNum > 1 Then
    15.             '~~~ Resize the array and append the contents from the begining to the pre-specified starting point
    16.             intCount = aa.GetUpperBound(0)
    17.             ReDim Preserve aa(intCount + (intNum - 1)) '~~~ *Edit: I have cleared a small mistake in this line
    18.  
    19.             For i = aa.GetLowerBound(0) To intNum - 2
    20.                 aa(intCount + i + 1) = aa(i)
    21.             Next
    22.         End If
    23.  
    24.         '~~~ Create the new string with each 3rd number
    25.         For i = intNum - 1 To aa.GetUpperBound(0) Step 3
    26.             strString2 = String.Concat(strString2, aa(i), ",")
    27.         Next
    28.  
    29.         MessageBox.Show(strString2)
    30.     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,...

  5. #5
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    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:
    1. Dim String1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    2.         Dim firstSplit() As String = String1.Split(","c)
    3.         Dim startingIndex As Integer = 5
    4.         Dim String2 As String = ""
    5.  
    6.         Do Until startingIndex > UBound(firstSplit)
    7.             If String2 = "" Then
    8.                 String2 &= firstSplit(startingIndex)
    9.             Else
    10.                 String2 &= "," & firstSplit(startingIndex)
    11.             End If
    12.             startingIndex += 3
    13.         Loop

    I'm not sure on the effectiveness of this method but try it and see what you think.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    83

    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

  7. #7
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: String manipulation

    Quote Originally Posted by Onenew View Post
    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,...

  8. #8
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] String manipulation

    vb.net Code:
    1. If String1.EndsWith(","c) Then
    2.    String1 = String1.SubString(0, String1.Length -1)
    3. End if


  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Function GetSubset(ByVal numbers As String, ByVal startIndex As Integer) As String
    2.     Dim superset As String() = numbers.Split(New Char() {","c}, _
    3.                                              StringSplitOptions.RemoveEmptyEntries)
    4.     Dim subset As New List(Of String)
    5.  
    6.     For index = startIndex To superset.GetUpperBound(0) Step 3
    7.         subset.Add(superset(index))
    8.     Next
    9.  
    10.     Return String.Join(",", subset.ToArray())
    11. End Function
    12.  
    13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.     Dim str1 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
    15.     Dim str2 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    16.  
    17.     MessageBox.Show(Me.GetSubset(str1, 1 - 1))
    18.     MessageBox.Show(Me.GetSubset(str2, 8 - 1))
    19. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Private Function GetSubset(ByVal numbers As String, ByVal startIndex As Integer) As String
    2.     Dim superset As String() = numbers.Split(New Char() {","c}, _
    3.                                              StringSplitOptions.RemoveEmptyEntries)
    4.     Dim subset As New List(Of String)
    5.  
    6.     For index = startIndex To superset.GetUpperBound(0) Step 3
    7.         subset.Add(superset(index))
    8.     Next
    9.  
    10.     Return String.Join(",", subset.ToArray())
    11. End Function
    12.  
    13. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.     Dim str1 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
    15.     Dim str2 = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25"
    16.  
    17.     MessageBox.Show(Me.GetSubset(str1, 1 - 1))
    18.     MessageBox.Show(Me.GetSubset(str2, 8 - 1))
    19. 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

  11. #11
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by J-Deezy View Post
    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,...

  12. #12
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by akhileshbc View Post
    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

  13. #13
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by J-Deezy View Post
    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,...

  14. #14
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: [RESOLVED] String manipulation

    Sorry mate, that'd clear out my bank account (unemployed 17-y/o ftw?)

  15. #15
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by J-Deezy View Post
    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,...

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    83

    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

  18. #18
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    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:
    1. Dim String1 As String = "11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,"
    2.         If String1.ToLower.EndsWith(",") Then
    3.             String1 = String1.Substring(0, String1.Length - 1)
    4.         End If
    5.         Dim str() As String = String1.Split(","c)
    6.         Dim totalElements As Integer = str.Count / 3
    7.         Dim StartingIndex As Integer = CInt(TextBox1.Text) - 1
    8.         Dim theList As New List(Of String)
    9.         Dim currentInt As Integer = StartingIndex
    10.  
    11.         Do
    12.             If theList.Count = totalElements Then Exit Do
    13.             If currentInt > str.Count Then currentInt -= str.Count
    14.             theList.Add(str(currentInt))
    15.             currentInt += 3
    16.         Loop
    17.  
    18.         Dim string2 As String = String.Join(",", theList.ToArray)
    19.         MsgBox(string2)

    It's pretty messy, I haven't got time to go through and clean it up so that's now your problem

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by akhileshbc View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] String manipulation

    @jmc - Winner, winner, chicken dinner.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  21. #21
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] String manipulation

    Quote Originally Posted by jmcilhinney View Post
    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,...

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Oct 2010
    Posts
    83

    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
  •  



Click Here to Expand Forum to Full Width