Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Split String by length

Hi guys, just a quick question one should go about splitting a string by length.

So basically an equivalent to:
Code
Dim str as string = split(somestring, ":")

but instead of the delimiter the string splits at a particular length

Thanks in advance
You are looking for substring.
You will find all the things you need at this link:
http://www.dotnetperls.com/substring-vbnet
(Jan 14, 2015 03:48 AM)KingDeath360 Wrote:  You are looking for substring.
You will find all the things you need at this link:
http://www.dotnetperls.com/substring-vbnet

aahh not quiet mate... i want to split strings to manage file sizes
(Jan 14, 2015 04:22 AM)Dark N3BULA Wrote:  aahh not quiet mate... i want to split strings to manage file sizes

Can you give an example of what you'll be splitting? I was going to recommend substring too.

Edit: I have to leave for school, I'll reply when I get back.
(Jan 14, 2015 04:28 AM)Odus Wrote:  Can you give an example of what you'll be splitting? I was going to recommend substring too.

Edit: I have to leave for school, I'll reply when I get back.

OP was attempting to send images from client to server earlier.
https://hackforums.net/showthread.php?tid=4634884

I believe the length of the base64 string from the original thread most likely exceeded the character limit and was cut off. Now OP wants to break the image into chunks, convert bytes to base64, and send multiple base64 strings to the server.
(Jan 14, 2015 04:37 AM)Hawk HF Wrote:  OP was attempting to send images from client to server earlier.
https://hackforums.net/showthread.php?tid=4634884

I believe the length of the base64 string from the original thread most likely exceeded the character limit and was cut off. Now OP wants to break the image into chunks, convert bytes to base64, and send multiple base64 strings to the server.

Umm basically split a base64 string at at the "boundry" or to have sub splits so theyre easier to work with
(Jan 14, 2015 04:37 AM)Hawk HF Wrote:  OP was attempting to send images from client to server earlier.
https://hackforums.net/showthread.php?tid=4634884

I believe the length of the base64 string from the original thread most likely exceeded the character limit and was cut off. Now OP wants to break the image into chunks, convert bytes to base64, and send multiple base64 strings to the server.

Umm basically split a base64 string at at the "boundry" or to have sub splits so theyre easier to work with
say I have a large text file encoded to base64 with length 110000. If there any way to split the encoded string into two bits with lengths 60000 and 70000
(Jan 14, 2015 05:15 AM)Dark N3BULA Wrote:  say I have a large text file encoded to base64 with length 110000. If there any way to split the encoded string into two bits with lengths 60000 and 70000

http://pastebin.com/pwTJDngq

Just call this with ReadFile("filename.txt", 70000, Encoding.UTF8).
Or whatever encoding/buffer length etc you want to use.
(Jan 14, 2015 06:10 AM)Dialog Wrote:  http://pastebin.com/pwTJDngq

Just call this with ReadFile("filename.txt", 70000, Encoding.UTF8).
Or whatever encoding/buffer length etc you want to use.

I found UTF8 a bit dodgy when working with images.

I figured that the following function might work
Code
Public Function SplitByLength(ByVal text As String, _
             ByVal length As Integer) As String()

      ' Argument validation elided
      Dim strArray As String() = New String((text.Length \ length) - 1) {}
      Dim i As Integer
      For i = 0 To text.Length - 1
      strArray(i) = text.Substring((i * length), length)
      Next i

      ' Get last array:
      ReDim Preserve strArray(i)
      strArray(i) = text.Substring(i * length)
      Return strArray
    End Function

Still havnt tried it out yet. Thanks for the input regardless 🎉
Dim S = "abcdefghi"
Dim A As New ArrayList
For i = 0 To S.Length - 1 Step 3
A.Add(S.Substring(i, 3))
Next
MsgBox(A(0))
(Jan 14, 2015 06:45 AM)Only3 Wrote:  Dim S = "abcdefghi"
Dim A As New ArrayList
For i = 0 To S.Length - 1 Step 3
A.Add(S.Substring(i, 3))
Next
MsgBox(A(0))

Could not get either to work.

S.Length is 86000
I put step as 10000

got the exception

System.ArgumentOutOfRangeException was unhandled
HResult=-2146233086
Message=Index and length must refer to a location within the string.
You put step at 10000 for what reason?
Ok finally worked it out.

Code
Public Function SplitBySize(ByVal InputString As String, ByVal StringSize As Integer) As String()
      Dim strA() As String
      Dim StringLength As Integer = InputString.Length()
      Dim iWords As Integer = StringLength / StringSize + IIf((StringLength Mod StringSize <> 0), 1, 0)
      ReDim strA(iWords)
      Dim j As Integer = 0, i As Integer
      For i = 0 To StringLength Step StringSize
      strA(j) = Mid(InputString, i + 1, StringSize)
      MsgBox(strA(j))
      j = j + 1
      Next i
      Return strA
    End Function

This works 🎉


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by SIM-2362662Apr 23, 2020 07:06 PM 11 375 Apr 23, 2020 07:06 PM
Last Post: dracx
[Python] by gurlhackerclubJan 29, 2020 02:48 AM 2 133 Jan 29, 2020 02:48 AM
Last Post: gurlhackerclub
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia