Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,924 questions

51,857 answers

573 users

How to divide a string into N equal parts with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Public Shared Function DivideStringIntoEqualParts(ByVal str As String, ByVal parts As Integer) As String()
        Dim length As Integer = str.Length

        If length Mod parts <> 0 Then
            Console.Write("No equal parts")
            Return Nothing
        End If

        Dim j As Integer = 0, part_size As Integer = length / parts
        Dim parts_arr As String() = New String(parts - 1) {}
        Dim i As Integer = 0

        While i < length
            parts_arr(j) = str.Substring(i, part_size)
            j += 1
            i = i + part_size
        End While

        Return parts_arr
    End Function

    Public Shared Sub Main(ByVal args As String())
		Dim str As String = "vb.net java c++ c python"
        Dim parts As Integer = 4
		
        Dim parts_arr As String() = DivideStringIntoEqualParts(str, parts)

        For i As Integer = 0 To parts_arr.Length - 1
            Console.WriteLine(parts_arr(i))
        Next
    End Sub
End Class




' run:
'
' vb.net
' java 
' c++ c 
' python
'

 



answered Oct 5, 2022 by avibootz

Related questions

2 answers 120 views
2 answers 141 views
2 answers 151 views
1 answer 115 views
1 answer 119 views
1 answer 112 views
1 answer 116 views
...