Question first four lines as separate strings

sheenitmathew

Member
Joined
Feb 4, 2013
Messages
9
Programming Experience
5-10
Hello

Thank you for spending your valuable time to read this.

I have an SQL Server varchar(500) filed that stores text from a multiline textbox. I want the first four lines as separate strings. The data is loaded to a datatable.

eg: dtFreshFruits.Rows(0).Item(24)--> here is my text

str1=first line
str2=second line
etc, etc...
 
Hi,

You can use the Split method of the String class to achieve this by splitting your value at the Carriage Return character. i.e:-

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  Dim DT As New DataTable
  Dim str1, str2, str3, str4 As String
 
  Dim myStringArray() As String = DT.Rows(0).Item(24).ToString.Split(CChar(vbCrLf))
  If myStringArray.Length >= 4 Then
    str1 = myStringArray(0)
    str2 = myStringArray(1)
    str3 = myStringArray(2)
    str4 = myStringArray(3)
  End If
End Sub

However, be cautious here, you say that this information has been saved from a Multiline TextBox which means that Carriage Returns HAVE to have been entered in the TextBox for this to work. If, what looks like Carriage Returns in the TextBox, are due to the Text Wrapping in the control then NO Carriage Return characters will have been saved with the data.

Hope that helps.

Cheers,

Ian
 
Back
Top