Hack Forums
[.NET] Split String by length - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Split String by length (/showthread.php?tid=4635983)



Split String by length - Dark N3BULA - Jan 14, 2015

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


* - KingDeath360 - Jan 14, 2015

You are looking for substring.
You will find all the things you need at this link:
http://www.dotnetperls.com/substring-vbnet


* - Dark N3BULA - Jan 14, 2015

(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


* - Odus - Jan 14, 2015

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


* - Hawk HF - Jan 14, 2015

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


* - Dark N3BULA - Jan 14, 2015

(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


* - Dark N3BULA - Jan 14, 2015

(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


* - Dark N3BULA - Jan 14, 2015

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


* - Dialog - Jan 14, 2015

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


* - Dark N3BULA - Jan 14, 2015

(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 🎉


* - Only3 - Jan 14, 2015

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))


* - Dark N3BULA - Jan 14, 2015

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


* - He B TeMy - Jan 14, 2015

You put step at 10000 for what reason?


* - Dark N3BULA - Jan 14, 2015

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 🎉