BP Forums
Problems with dividing a string - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: Problems with dividing a string (/showthread.php?tid=816)



Problems with dividing a string - brco900033 - 10-19-2013

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


Re: Problems with dividing a string - brandonio21 - 10-20-2013

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!


Re: Problems with dividing a string - brco900033 - 10-21-2013

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.


Re: Problems with dividing a string - brandonio21 - 10-23-2013

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!


Re: Problems with dividing a string - brco900033 - 10-25-2013

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


Re: Problems with dividing a string - brandonio21 - 10-26-2013

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]



Re: Problems with dividing a string - brco900033 - 10-26-2013

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