Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control number of lines in text 2

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
0
0
US
Hi,
I have a string of text that has line breaks as <br /> characters. I need to change the string to only contain 3 lines of text (or 2 <br /> characters).
What is the way to do this?

thanks!!!
 
You could do something like this:

Code:
Dim stringHTML As String = "this is line 1 <br/> this is line 2 <br/> this is line 3 <br/>"
Dim atChar = stringHTML.LastIndexOf("<br/>")
stringHTML = stringHTML.Remove(atChar, 5)

Kevin Davie
Consultant
Sogeti USA
 
KDavie, not sure I understand this line:
stringHTML = stringHTML.Remove(atChar, 5)

why 5?
 
oops... I meant:

<br/>

Also, I noticed in your question you have an added space. You may want to find then index of '>' from atChar and use that to determine the length. It would probably be less prone to failure.

-Kevin

Kevin Davie
Consultant
Sogeti USA
 
still... this code just removes the last <br /> character. That is not what needs to be done
Imagine a string
Dim stringHTML As String = "this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4<br />..."

I need to change stringHTML to only contain
"this is line 1 <br/> this is line 2 <br/> this is line 3 <br/>"

The number of <br /> is nto known initially, there coudl be 100 of them

thanks
 
You can do something like this:

Code:
Dim stringHTML As String = "this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4<br />"

Dim atChar As Integer = 0
Dim i As Integer = Nothing
For i = 1 To 3
     atChar = (stringHTML.IndexOf("<br/>", atChar) + 5)
Next

Dim newString As String = stringHTML.Substring(0, atChar)

Kevin Davie
Consultant
Sogeti USA
 
developer155,

Code:
	Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
		MessageBox.Show(StripLines("this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4 <br/> this is line 5 <br/>", 3))
		MessageBox.Show(StripLines("this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4 <br/> this is line 5 <br/>", 10))
		MessageBox.Show(StripLines("this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4 <br/> this is line 5 <br/>", 0))
		MessageBox.Show(StripLines("this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4 <br/> this is line 5 <br/>", -1))
		MessageBox.Show(StripLines("this is line 1 <br/> this is line 2 <br/> this is line 3 <br/> this is line 4 <br/> this is line 5 <br/>", 2))
	End Sub

[COLOR=red][b]
	Private Function StripLines(ByVal stringHtmlArg As String, ByVal NumberOfLinesRequired As Integer) As String
		Dim separator As String = "<br/>"
		Dim stringHTML As String = String.Empty
		Dim htmlLines() As String

		Try
			htmlLines = stringHtmlArg.Split(separator, NumberOfLinesRequired + 1, StringSplitOptions.RemoveEmptyEntries)
			stringHTML = String.Join(separator, htmlLines, 0, NumberOfLinesRequired)
		Catch ex As ArgumentOutOfRangeException
			' INFO: Value of NumberOfLinesRequired is out of range. Do nothing.
		End Try

		Return (stringHTML)
	End Function
[/b][/color]

It is so easy that you can change it according to your dynamic requirements e.g. changing the number of lines in future.

Hope it helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top