Posts: 96
Threads: 39
Joined: Sep 2011
Reputation:
0
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
Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
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!
Posts: 96
Threads: 39
Joined: Sep 2011
Reputation:
0
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.
Posts: 1,006
Threads: 111
Joined: Jul 2010
Reputation:
1
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!
Posts: 96
Threads: 39
Joined: Sep 2011
Reputation:
0
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].
Posts: 96
Threads: 39
Joined: Sep 2011
Reputation:
0
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