remove double linebreaks in string - asp.net

I am developing a asp.net web application, i have a string (with a value in it from a database), with multiple lines that i put in a TextBox with mulitline type. (textarea)
Now the problem is, that in the string are multiple lines, with much empty space. so i want the remove only the double linebreaks.
example of my textbox:
+++++++++++++++++++++++++++++++++++++++++++++++++++++
{empty}
{empty}
'This is some text in the textbox on line 3
'some text on line 4
{empty}
'some text on line 6
{empty}
{empty}
'some text on line 9
{empty}
+++++++++++++++++++++++++++++++++++++++++++++++++++++
now somehow i want to remove line 1 and 2, and line 7 and 8
thanks in advance

Here is the solution:
'now rebuild your example string
Dim Empty As String = Chr(13) & Chr(10)
Dim Sb As New System.Text.StringBuilder
Sb.Append("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append(Empty & "This is some text in the textbox on line 3")
Sb.Append(Empty & "some text on line 4")
Sb.Append(Empty)
Sb.Append(Empty & "some text on line 6")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append(Empty & "some text on line 9")
Sb.Append(Empty)
Sb.Append(Empty)
Sb.Append("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
Dim YourString As String = Sb.ToString
MessageBox.Show(YourString)
'now replace the double empty
Dim result As String
result = YourString.Replace(Empty & Empty & Empty, Empty)
MessageBox.Show(result)
NOTE: This solution has been tested OK with Visual Studio 2010.

This will get rid of all empty lines.
Dim splt() As Char = New Char() {ControlChars.Lf, ControlChars.Cr}
Dim lines() As String = TextBox1.Text.Split(splt, StringSplitOptions.RemoveEmptyEntries)
TextBox1.Lines = lines
This looks like it will get rid of multiple newlines
Dim s As String = TextBox1.Text.Replace(Environment.NewLine, ControlChars.Cr)
Dim lines As New List(Of String)
lines.AddRange(s.Split(New Char() {ControlChars.Cr}))
For x As Integer = lines.Count - 1 To 1 Step -1
If lines(x) = "" AndAlso lines(x - 1) = "" Then
lines.RemoveAt(x)
End If
Next
TextBox1.Lines = lines.ToArray

The way I usually do this is to convert all of the various line breaks into a single one that I can manage, de-dupe and convert back to vbNewLine:
'//Convert all line break types to vbCr/ASCII 13
T = T.Replace(vbNewLine, vbCr).Replace(vbLf, vbCr)
'//Loop until all duplicate returns are removed
Do While T.Contains(vbCr & vbCr)
T = T.Replace(vbCr & vbCr, vbCr)
Loop
'//Check to see if the string has one at the start to remove
If T.StartsWith(vbCr) Then T = T.TrimStart(Chr(13))
'//Convert back to standard windows line breaks
T = T.Replace(vbCr, vbNewLine)

The following code removes double empty lines at the beginning, and also double empty lines anywhere in the textbox.
Dim myText as String = TextBox1.Text
myText = Regex.Replace(myText, "^(\r\n\r\n)(.*)", "$2")
myText = Regex.Replace(myTextt, "(.*\r\n)(\r\n\r\n)(.*)", "$1$3")
TextBox1.Text = myText
In the example given, it would remove lines 1 and 2, and lines 7 and 8.

Related

Creating a label inside Gridview Cell from VB

I have some Code in VB that is looping through a gridview's rows and is checking the values of a certain collumn, I have written some code inside the if statement that creates a clickable label inside the cell for when the cell is "", I have already written a bit of code that could create my label but I'm not too sure how it would create it inside row.Cells(8)
I was wondering if could get some assistance with how I should be doing this?...
Here's my Code:
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim row = GridView1.Rows(i)
Dim rowHeaderCell = row.Cells(8)
If rowHeaderCell.Text = " " Then
Dim lbl As New Label
lbl.Size = New System.Drawing.Size(159, 23)
lbl.Location =
lbl.Text = "label text goes here"
Me.Controls.Add(lbl)
End If
Next
Thankyou in advance!
To make a clickable label change the type of the column to : [DataGridViewLinkColumn]
here is the code to replace the Empty cell value by the value of LBL:
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).Cells(8).Value.Trim = "" Then
Dim lbl As String
lbl = "label text goes here "
GridView1.Rows(i).Cells(8).Value = lbl
End If
Next

Cannot get the last row of data

In my ASP.NET application, i need to read the CSV file and insert into sql server.
and it was strange that it treat the 1st row (column name) as LineNumber=2.
but i found that my code cannot read the last row of the CSV file.
Dim CSV_content As String = ""
Dim CSVFilePathName As String = Server.MapPath("~/XXX/" & filename)
If File.Exists(CSVFilePathName) = True Then
'Response.Write("exists")
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(CSVFilePathName)
Dim CurrentRecord As String()
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
Dim LastName As String = ""
Dim FirstName As String = ""
Dim DisplayName As String = ""
Dim EmailName As String = ""
Dim dc As New dcHRISDataContext
' parse the actual file
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
'insert into tmp db
If afile.LineNumber > 2 Then
Dim newRecord1 As New XXX
dc.XXX.InsertOnSubmit(newRecord1)
newRecord1.LastName = CurrentRecord(0).Trim
newRecord1.FirstName = CurrentRecord(1).Trim
newRecord1.DisplayName = CurrentRecord(1).Trim
newRecord1.DirEmail = CurrentRecord(3).Trim
newRecord1.GetFileDate = DateTime.Now
newRecord1.GetFileName = filename
dc.SubmitChanges()
End If
Catch ex As FileIO.MalformedLineException
Stop
End Try
Loop
End If
Wow, this is the dumbest issue that I have seen.
Anyhow, Apparently the last row in the TextFieldParser is -1, and not LastRowWithValuesIndex + 1. So what happens is this.
You read the data of the last line (this is fine)
The reader skips to the next line to prepare the next line to read (this is fine)
You get the afile.LineNumber, now set to the next line. Which is -1
at this point. (this is dumb although understandable, not your fault)
Your if statement wont let you in because hey you are on line -1.
Loop ends, skipping last row in the CSV.
All you have to do is to read the line number of the row before reading the data:
Dim i As Integer = afile.LineNumber
CurrentRecord = afile.ReadFields
If i > 2 Then
etc

string replace not working at all

I am reading a text file and I need to replace a few areas with new text that I marked with {0}, {1}, {2}, {3}, {4}, {5}, {6}.
So I load in the text, save it to a string variable, and then use String.Replace but it's not working.
For i As Integer = 0 To 6
fileText.Replace("{" & i & "}", DisplayStudentData(i))
Next
And DisplayStudentData looks like this:
Protected Function DisplayStudentData(ByVal itemNumber As Integer) As String
Dim dsItem As String = ""
If itemNumber <> -1 Then
Select Case itemNumber
Case 0
dsItem = "testFirstName"
Case 1
dsItem = "testTitle"
Case 2
dsItem = "testClass"
Case 3
dsItem = "testTeacher"
Case 4
dsItem = "testDept"
Case 5
dsItem = "testEmail"
Case 6
dsItem = "testPhone"
End Select
End If
Return dsItem
End Function
It seems like the above should work, but it doesn't.
Any help would be greatly appreciated.
Thanks!
Ok I figured it out...
I put all the data items into an array, then did this after loading the text file:
fileText = String.Format(fileText, dArr(0), dArr(1), dArr(2), dArr(3), dArr(4), dArr(5), dArr(6))
Is this a good way of doing it?
fileText = fileText.Replace("{" & i & "}", DisplayStudentData(i))
Replace returns a new string. It does not modify the string from which it was called.
You could also do this:
fileText = String.Format(fileText, Enumerable.Range(0,7).Select(Function(i) DisplayStudentData(i)).ToArray())
Why isn't DisplayStudentData just an array in the first place (which would make string.Format() even easier)?
on another note, If you know the string will always stay the same
you could do something like
dim myTestString = "{0} {1} {2} {3} {4} {5} {6}"
dim result as string = string.format(myTeststring, "testFirstName",
"testTitle", "testClass", "testTeacher", "testDept", "testEmail", "testPhone")
you could always make it a property of your studentdata object.

loop through datatable to create a string that looks like this

I want to loop through my datatable column called SDESCR and created a string that looks like this.
Dim labels As String() = {"North", "South", "East", "West", "Up", "Down"}
this is what i am trying and it is not working
Dim labels As String()
For Each row As DataRow In tablegraph.Rows
labels = labels " ' " + row.Item("SDESCR") + " ',"
Next row
THANK YOU FOR THE HELP, I WILL TEST THEM TOMORROW AND SEE WHICH WORKS BEST AND MARK ONE CORRECT.
Do this instead
Dim labels As String()
Dim labelsList As List(Of String) = New List(Of String)
For Each row As DataRow In tablegraph.Rows
labelsList.Add(row.Item("SDESCR"))
Next
labels = labelsList.ToArray()
If you need it to be a comma delimited list instead you can just do
Dim commaLabels As String = String.Join(labels, ",")
If you need them stored in an array, then you can add them to a list and then call the ToArray() method on the list:
Dim labelList As New List(Of String)
For Each row As DataRow In tablegraph.Rows
labelList.Add(row.Item("SDESCR"))
Next row
Dim labels As String() = labelList.ToArray()
If you want a string, use this (string builder is best due to memory management issues with constant string declaration.
Dim labels As New StringBuilder()
For Each row As DataRow In tablegraph.Rows
labels.Append(" ' " + row.Item("SDESCR") + " ',")
Next row
Then when you need the string, use labels.ToString()
If you want an array, use this...
Dim labels As New List(Of String)()
For Each row As DataRow In tablegraph.Rows
labels.Add(row.Item("SDESCR"))
Next row
Then when you need the array, use labels.ToArray()
The main reason your above code is failing is that you are declaring an Array of strings.
If you mean a String-Array instead of a String, this works:
Dim labels(tablegraph.Rows.Count - 1) As String
For i As Int32 = 0 To tablegraph.Rows.Count - 1
labels(i) = tablegraph(i)("SDESCR").ToString
Next
If you want them separated with a comma, you can use a StringBuilder to append:
Dim labels As New System.Text.StringBuilder
For i As Int32 = 0 To tablegraph.Rows.Count - 1
labels.Append(" ' ").Append(tablegraph(i)("SDESCR").ToString).Append(" ',")
Next
If labels.Length <> 0 Then labels.Length -= 1 'remove last comma'
Dim labelString As String = labels.ToString
There are plenty of ways to do this; here is an approach using LINQ
Dim labels As String() = (From myRow In tablegraph _
Select CType(myRow.Item("SDESCR"), String)).ToArray
It's worth nothing that this will fail for NULL/NOTHING values of "SDESCR".
If you just want a single comma-separated values you can do this:
Dim sOutput = String.Join(",", (From myRow In tablegraph _
Select CType(myRow.Item("SDESCR"), String)).ToArray)
labels = labels " ' " + row.Item("SDESCR") + " ',"
^
Plus sign missing?
Also, when you do this you'll be left with an extra , at the end of your string. So you'll want to strip that off.

fusion free charts and arraylist

i am trying to use an arraylist to build a graph using fusionfree
however i cant get it to work, it keeps showing the same item from the array list
Dim util As New Util()
Dim region As New List(Of String)
Dim int1 As New List(Of Integer)
For Each rows As GridViewRow In GridView1.Rows
' Selects the text from the TextBox
Dim regions As Label = CType(row.FindControl("label1"), Label)
region.Add(Label1.Text)
int1.Add(10)
Next
Dim strXML As String, i As Integer
'Initialize <graph> element
strXML = "<graph caption='Sales by Product' numberPrefix='$' formatNumberScale='0' decimalPrecision='0'>"
'Convert data to XML and append
Dim x As Int32
x = 0
For i = 0 To (region.Count) - 2
x = (x + 1)
'add values using <set name='...' value='...' color='...'/>
strXML = strXML & "<set name='" & region.Item(x) & "' value='" & int1.Count & "' color='" & util.getFCColor() & "' />"
Next
'Close <graph> element
strXML = strXML & "</graph>"
'Create the chart - Column 3D Chart with data contained in strXML
Return FusionCharts.RenderChart("../FusionCharts/FCF_Column3D.swf", "", strXML, "productSales", "600", "300", False, False)
In here, you are adding the same values over and over:
For Each rows As GridViewRow In GridView1.Rows
' Selects the text from the TextBox
Dim regions As Label = CType(row.FindControl("label1"), Label)
region.Add(Label1.Text) 'This should maybe be regions? You are always using Label1
int1.Add(10)
Next
You are adding the Text property from Label1 (not the current row's label1) and 10 to region and int1 resp.
Also, is row declared somewhere above the loop above? I just noticed that your loop variable is rows, but you are trying to pull label1 from row. Can you please clarify this discrepancy?
You could try http://liberofusioncharts.codeplex.com/. Make things simpler

Resources