none
Substring large text RRS feed

  • Question

  • Hi Guys,

    I've got this string, its a very very large string. For instance lets say, around 100.000 characters located in a textbox on the form.
    Now i need to splitt this string in parts of 1000 characters.

    Im not sure if this is the right way to do this.. It looks like its working, but i'm pretty sure the parts are not correct.

                Dim CharactersInData As Integer = DataBox.TextLength
                Dim DataFile As String = DataBox.Text
    
    
                Dim StartCutter As Integer = "0"
                Dim EndCutter As Integer = "999"
    
    LoopStart:
              
                Try
                    Dim PartOfString As String = DataFile.Substring(StartCutter, EndCutter)
                    
                    msgbox(PartOfString)
                    StartCutter = StartCutter + "1000"
                    EndCutter = EndCutter + "1000"
    
                    GoTo LoopStart
                Catch ex As Exception 
    
    
                   Dim PartOfString As String = DataFile.Substring(EndCutter)
                    msgbox(PartOfString)
                    
                End Try
    
    

    I hope someone could help me out :)

    Greetings,

    Kevin



    Saturday, July 14, 2018 8:16 PM

Answers

  • Hi

    Here is one of the many ways to get it. This example fills a List with all the sub strings found.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' create a long string for test
        Dim s As String = "AB12345CDEx"
        Do
          s &= s
        Loop Until s.Length > 100000
    
        ' determin substrings
        Dim sectionsize As Integer = 1000
        Dim remainder As Integer = s.Length Mod sectionsize
        s = s.Substring(0, s.Length - remainder)
        Dim PartsOfString As New List(Of String)
        For i As Integer = 0 To s.Length - sectionsize Step sectionsize
          PartsOfString.Add(s.Substring(i, 1000))
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, July 14, 2018 10:25 PM

All replies

  • Look again at the documentation. The second argument is a length.


    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 14, 2018 10:09 PM
  • Hi

    Here is one of the many ways to get it. This example fills a List with all the sub strings found.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' create a long string for test
        Dim s As String = "AB12345CDEx"
        Do
          s &= s
        Loop Until s.Length > 100000
    
        ' determin substrings
        Dim sectionsize As Integer = 1000
        Dim remainder As Integer = s.Length Mod sectionsize
        s = s.Substring(0, s.Length - remainder)
        Dim PartsOfString As New List(Of String)
        For i As Integer = 0 To s.Length - sectionsize Step sectionsize
          PartsOfString.Add(s.Substring(i, 1000))
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, July 14, 2018 10:25 PM
  • Modify code

     Dim EndCutter As Integer = "1000"

    Delete code 

    EndCutter = EndCutter + "1000"

    https://msdn.microsoft.com/en-us/library/system.string.substring.aspx

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Monday, July 16, 2018 2:52 AM
  • Modify code

     Dim EndCutter As Integer = "1000"

    Delete code 

    EndCutter = EndCutter + "1000"
    Kevin/Alex, Integers should not have quotation marks around the value.

    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.



    Monday, July 16, 2018 3:00 AM
    Moderator
  • Hi Paul

    You are right, but it seems to be no problem to run.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 16, 2018 3:04 AM
  • Try something like this:

        Function SplitString(str As String, chunkSize As Integer) As List(Of String)
            Dim result As New List(Of String)
            For i As Integer = 0 To str.Length - 1 Step chunkSize
                Dim sZ As Integer = If(i + sZ > str.Length, str.Length Mod chunkSize, chunkSize)
                Dim chunk As String = str.Substring(i, sZ)
                result.Add(chunk)
            Next
            Return result
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim size As Integer = 5012
            Dim s As String = "".PadLeft(size, "A"c)
            For Each s2 As String In SplitString(s, 1000)
                MsgBox(s2.Length)
            Next
        End Sub


    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.



    Monday, July 16, 2018 3:16 AM
    Moderator
  • Hi Paul

    You are right, but it seems to be no problem to run.

    Best Regards,

    Alex

    This is because option strict is off... The compiler is assuming that an error has been made(and correctly resolving the problem), but this isn't a good practice as Visual Basic is a Typed language, meaning you should use the correct data type when assigning a literal value. In this particular instance Visual Basic sees the quotation marks and is able to accurately assume that you accidentally put an integral value within a string, and when checking the content of the string, Visual Basic indeed sees an integral type, and therefore can assume you meant to wrap the string with the CInt function, hence calling an extra layer of code to be executed to convert your string "1000" to the correct datatype. The extra layer being the call to the CInt function, which is not necessary if the variable is declared correctly. This will however so slight, cause a reduction in performance.

    Hire Me For This Job!
    Don't forget to vote for Helpful Posts and Mark Answers!
    *This post does not reflect the opinion of Microsoft, or its employees.



    Monday, July 16, 2018 3:21 AM
    Moderator
  • Modify code

     Dim EndCutter As Integer = "1000"

    Delete code 

    EndCutter = EndCutter + "1000"

    https://msdn.microsoft.com/en-us/library/system.string.substring.aspx


    Kevin is a smart person. Certainly he figured that out as soon as he saw that he overlooked that the second argument is a length. Kevin however might not yet be comfortable with the procedures here.


    Sam Hobbs
    SimpleSamples.Info

    Monday, July 16, 2018 3:49 AM
  • Check Regular Expressions too:

    For Each m As Match In Regex.Matches(DataFile, ".{1,1000}")
       Dim PartOfString As String = m.Value
       MsgBox(PartOfString)
    Next
    

    Monday, July 16, 2018 5:58 AM
  • Hi

    Here is one of the many ways to get it. This example fills a List with all the sub strings found.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' create a long string for test
        Dim s As String = "AB12345CDEx"
        Do
          s &= s
        Loop Until s.Length > 100000
    
        ' determin substrings
        Dim sectionsize As Integer = 1000
        Dim remainder As Integer = s.Length Mod sectionsize
        s = s.Substring(0, s.Length - remainder)
        Dim PartsOfString As New List(Of String)
        For i As Integer = 0 To s.Length - sectionsize Step sectionsize
          PartsOfString.Add(s.Substring(i, 1000))
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thanks les,

    This did the trick!.


    Tuesday, July 17, 2018 12:29 AM