How to Find the Number of Lines in a String for Visual Basic

Save

There is no method in the Visual Basic string class to find the number of lines in a multi-line string. However, with a little creative use of the existing methods, the value can still be discovered. The key is to note that the "vbNewLine" constant can be used as input for the "split" method. This will give you an array of all the lines in the string, and from there you need only to check the size of the array to see the string length in lines.

  • Open Visual Basic. Create a new project by clicking "File," "New Project." Select "Console application."

  • Paste the following code within the application:

        Dim s = "This is a " & vbNewLine & " multiline string."

    MsgBox(s)

    Dim t = s.Split(vbNewLine)

    MsgBox(t.Length)

    This defines a string with two lines, shows it to the user, splits it wherever there is a line break and shows you the total line count.

  • Click "Run" to test your application.

References

Promoted By Zergnet

Comments

You May Also Like

Related Searches

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!