Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with dividing a string
#1
I have already another question, I'm not asking how to do something for a change.

I have written something but it seems that it doesn't work properly and I don't know what the problem is.

The purpose of the code is to divide a key (length of key can varie) into parts of each 4 characters.
[code2=vbnet]Dim strings As New List(Of String)
'Just an example of the key, this string will be generated randomly
Dim s As String = "p6s0arJHmuOQmUhIczfnynRS1dOrRxYm0EF05b5Vk1YR7TQVItShNjFJQ1L3d"
Dim i As Integer = 0
Dim intMaxNumber As Integer = CInt(Math.Round((s.Length / 4), 0))
For i = 0 To intMaxNumber Step 4
strings.Add(s.Substring(i, 4))
Next
For Each srtininglist As String In strings
MessageBox.Show(srtininglist)
Next[/code2]
Everything goes fine when I debug it but it seems that the code only returns 4 parts, and it should (in this case) be about 16 parts.

I'm probably making a logical mistake but I can't figure out what I'm doing wrong.


Hope someone can help me out,
Brecht
#2
The problem lies within this line of your code:
[code2=vbnet]For i = 0 To intMaxNumber Step 4[/code2]

Since intMaxNumber is already the length of the String divided by 4, you are essentially dividing it by four again by stepping by 4 each time. To fix this, you just need to make sure that you are going through the whole string. You can do this by removing the intMaxNumber variable entirely and replacing the logic with something like this:
[code2=vbnet]For i = 0 To s.Length Step 4
strings.Add(s.Substring(i, 4))
Next[/code2]

That should do it!
My Blog | My Setup | My Videos | Have a wonderful day.
#3
It gives me an error: ArgumentOutOfRangeException
description: Index and length must refer to a location within the string.
Parameter name: length

This error occurrs on the line
[code2=vbnet]listParts.Add(inputKey.Substring(i, 4))[/code2]

Could it be that the problem is the length of the key? It is like 115 characters long (don't know the exact length but what I do know is that the length is not a round number), so you can't divide it by 4.
#4
This would be expected when we reach the end of the string since its length is not divisible by 4. For example, there might be 3 characters left, but we are trying to grab the next four. What you can do to prevent this is know exactly when we run out of sequences of 4 characters and subtract the amount we need. For example,
[code2=vbnet]Dim remainder as Integer = s.Length Mod 4
For i = 0 To (s.Length - remainder) Step 4
strings.Add(s.Substring(i, 4))
Next
strings.Add(s.Substring(s.length - remainder, remainder)[/code2]

Essentially, this divides the string by 4 and gets the remainder, which is how many characters are left over when the error is thrown. To prevent the error from being thrown, we only move up to the last slot divisible by 4 in the For loop and then we add on the remaining integers at the end.


Let me know if this works or if you have any questions!
My Blog | My Setup | My Videos | Have a wonderful day.
#5
I'm not sure if I'm using the code the right way but I get the same error, again on the same line:
[code2=vbnet]strings.Add(s.Substring(i, 4))[/code2]

The code that I have now is the following:
[code2=vbnet]Dim strings As New List(Of String)
'Just an example of the key, this string will be generated randomly
Dim s As String = "p6s0arJHmuOQmUhIczfnynRS1dOrRxYm0EF05b5Vk1YR7TQVItShNjFJQ1L3d"
Dim i As Integer = 0
Dim remainder As Integer = s.Length Mod 4
For i = 0 To (s.Length - remainder) Step 4
strings.Add(s.Substring(i, 4))
Next
strings.Add(s.Substring(s.Length - remainder, remainder))
For Each srtininglist As String In strings
MessageBox.Show(srtininglist)
Next[/code2]

Am I doing something wrong because I still get the same error?

PS: What's the html code for the 'vbnet code' block? I still use the old one [code][//code].
#6
Woops! So the problem here lies in the fact that when scanning through a list of objects, the last object is (.Length - 1), thus, in your for loop
[code2=vbnet]For i = 0 To (s.Length - remainder) Step 4[/code2]
You are scanning 1 object over the end of the array. To prevent this, you can simply tag on a "- 1". Like so:
[code2=vbnet]For i = 0 To (s.Length - remainder - 1) Step 4[/code2]
And that should fix everything, if not, let me know!

And the new VB.NET Syntax is as follows
Code:
[code2=vbnet]CODE[/code2]
My Blog | My Setup | My Videos | Have a wonderful day.
#7
Thanks for the help! It works perfectly now!

The final code is below, the function divides the string after the amount of characters you want and with the delimiter you want:

[code2=vbnet]Public Function DivideString(ByVal strKey As String, ByVal intPartLength As Integer, ByVal strDivider As String) As String
Dim keyParts As New List(Of String)
Dim remainder As Integer = strKey.Length Mod intPartLength
For i = 0 To (strKey.Length - remainder - 1) Step intPartLength
keyParts.Add(strKey.Substring(i, intPartLength))
Next
keyParts.Add(strKey.Substring(strKey.Length - remainder, remainder))
Dim strDividedString As String = String.Empty
For Each strPart As String In keyParts
strDividedString = strDividedString & strPart & strDivider
Next
If strDividedString.EndsWith(strDivider) Then Return strDividedString.Trim(CChar(strDivider)) Else Return strDividedString
End Function[/code2]

Thanks again,
Brecht


Possibly Related Threads…
Thread Author Replies Views Last Post
  Search ListBox + Convert ListBox.item to string brco900033 3 14,583 09-13-2012, 03:03 PM
Last Post: brandonio21
  How to write string query on multiple lines kismetgerald 1 8,960 08-29-2012, 04:01 PM
Last Post: brandonio21
  VB.Net Query Builder Problems. Moldraxian 2 12,538 08-17-2012, 05:33 AM
Last Post: Moldraxian
  Outputting a string with quotation marks in it. openeXpressions 15 47,664 12-20-2011, 09:24 PM
Last Post: openeXpressions
  Problems with multiple textboxes and coding LearningVB2010 30 92,850 02-28-2011, 07:39 AM
Last Post: brandonio21

Forum Jump:


Users browsing this thread: 1 Guest(s)