I am trying to replace all the occurrences of "\n" in the Text property of an ASP.NET TextBox with <br /> using String.Repalce function, but it doesn't seem to work:
taEmailText.Text.Replace("\n", "<br />")
As a solution I am using Regex.Replace:
New Regex("\n").Replace(taEmailText.Text, "<br />")
My question is why String.Replace can't find "\n" for me, even though this solution has been proposed on many sites and it has worked for many people.
Thanks,
In .NET string objects are immutable, so String.Replace returns a new string with the replacement. You need to assign the result:
taEmailText.Text = taEmailText.Text.Replace("\n", "<br />")
Also, rather than creating a new Regex object—when you do need a regular expression—then there are static methods available:
result = Regex.Replace(input, pattern, replacement)
Edit (based on comment):
Just tested this:
Sub Main()
Dim result As String = "One\nTwo".Replace("\n", "<br />")
Console.WriteLine(result)
End Sub
and the result is:
One<br />Two
The problem is the result of the method call is immediated forgotten. You should read MSDN documentation a bit more carefully:
Returns a new string in which all occurrences…
Hence do:
taEmailText.Text = taEmailText.Text.Replace("\n", "<br />")
Replace does not change the content of the input string. It returns newly created string.
You might want to replace both \r and \n or use Environment.NewLine constant.
var replacedText = TextBox1.Text.Replace(Environment.NewLine, "<br />");
Related
I am trying to build a string that includes a newline character and is getting the weirdest result. This string is going to be stored in a SQL database to be later used as part of an email. My code is the following:
Dim strBody As String = "Andy," & Environment.NewLine
When I inspect the value of strBody during a debugging session, it is the following:
"Andy," & vbCrlf
I am obviously expecting is to be more like:
"Andy,"
Knowing that what is after the , is a hidden character.
Ultimately, the problem is... when I include strBody as part of my SQL insert statement, it literally shows up as the following within my SQL insert statement:
'Andy," & vbCrLf & "'
I was using this code yesterday and it worked fine. I am using similar code within another function of the same asp.net project and it works fine. I have tried using + instead of &, I have tried to use vbCrLf instead of Environment.NewLine, I have tried using stringbuilder, I have tried using string.concat. All with the same results where the & vbCrLf is included in strBody.
Is there a setting that I accidentally changed?
I know this is a weird one... Any help is greatly appreciated.
This is only Visual Studio showing you that there is new line character (vbCrLf or Environment.NewLine). If you use that string anywhere, it will be stored correctly.
I believe you will need to use some combination of Char(10) and Char(13):
Dim strBody As String = "'Andy,'" & " + CHAR(13)+CHAR(10) + "
There is a discussion about these and other methods on this thread.
You can do like this if you just need to insert Environment.NewLine inside database.
Dim a As String = """ & Environment.NewLine & """
Dim strBody As String = String.Format("Andy,{0}", a)
'"Andy," & Environment.NewLine & ""
I'm building a app that I need to catch the text of textarea with all lines separated. That is, if the user typed in textarea and press "Enter" to do a new line. Is there a way I can get the text from textarea and know the places where the user lines separated by "Enter"?
From my tests, in Internet Explorer I can get using Environment.NewLine:
string someString = TextArea1.Text.Replace(Environment.NewLine, "<br />")
But using Firefox or Chrome did not work.
try this:
string someString = TextArea1.Text.Replace( Convert.ToString(Convert.ToChar(13)) + Convert.ToString(Convert.ToChar(10)), "<br />")
edit
thats the final solution after trying some others.
please look at history of this question for more solutions. :)
I ran my own test, and this worked for me.
TextBox1.Text = TextBox1.Text.Replace("\r\n","<br />");
Something else is wrong if it is not for you.
I have also had problems with "\r\n" in the past. However, this has always seemed to work for me:
string someString = TextArea1.Text.Replace("\n", "<br />");
I also like to be anal about making sure nothing gets missed so I often do:
string someString = TextArea1.Text.Replace("\r\n", "<br />").Replace("\n", "<br />");
But the first one should work fine.
Good Luck!
I want to make sure using the custom expression validator that no ' or " can be used i tryed using [^"'] but whenever i put normal letters it doesnt work either.
I would just do a regex replace to match ['"] and replace it with nothing (ie. "").
In VB.NET, the code would look like:
dim strTest as string = "This "is" a test'"
dim regex as regex = new regex("[" & chr(34) & "']", regexoptions.ingorePatternWhitespace)
msgbox(regex.Replace(strTest, ""))
The output will be This is a test.
Hi
I am trying to submit some information into a database using an ASP.NET multiline textbox.
I have the following code:
Dim noteContent As String = Replace(txtNoteContent.InnerText.ToString(), vbCrLf, "<br />")
Shop.Job.Notes.addNote(Request.QueryString("JobID"), ddlNoteFrom.SelectedValue, ddlNoteTo.SelectedValue, noteContent)
The addNote's function code is:
Public Shared Sub addNote(ByVal JobID As Integer, ByVal NoteFrom As Integer, ByVal NoteTo As Object, ByVal NoteContent As String)
Dim newNote As New Job_Note
newNote.Date = DateTime.Now()
newNote.JobID = JobID
newNote.NoteByStaffID = NoteFrom
If Not NoteTo = Nothing Then
newNote.NoteToStaffID = CInt(NoteTo)
End If
newNote.NoteContent = NoteContent
Try
db.Job_Notes.InsertOnSubmit(newNote)
db.SubmitChanges()
Catch ex As Exception
End Try
End Sub
When it submit's the compiler does not seem to detect that line breaks have been entered into the textbox. I have debugged and stepped through the code, and sure enough, it just ignores the line breaks. If I try and replace another character instead of a line break, it works fine.
It doesn't seem to be anything to do with my function, the LINQ insert or anything like that, it simply just doesn't detect the line breaks. I have tried VbCr, and also chr(13) but they do not work either.
Can someone help me? I have been trying ad searching for over an hour trying to sort this.
Thanks
When you do your replace, you should check for VbCrLf (Windows Line Break), VbLf (Unix Line Break) and VbCr (Mac Line Break). If memory serves correct, the standard newline in a HTML textarea element is "\n" (aka VbLf), so you might just get away with replacing VbCrLf with VbLf in your code, but personally I always check for them all just to be safe.
Example
Dim htmlBreakElement As String = "<br />"
Dim noteContent As String = txtNoteContent.Text _
.Replace(vbCrLf, htmlBreakElement) _
.Replace(vbLf, htmlBreakElement) _
.Replace(vbCr, htmlBreakElement)
Shop.Job.Notes.addNote(Request.QueryString("JobID"), ddlNoteFrom.SelectedValue, ddlNoteTo.SelectedValue, noteContent)
I use the split function of in VBScript to split the string. Below is the code am using.
Dim inputText
DIM resultArray
inputText = "abc; def; ""xyz;123"""
resultArray = Split(inputText, "; ")
For i = 0 To UBound(resultArray)
resultArray(i) = Replace(resultArray(i), """", "")
resultArray(i) = Replace(resultArray(i), ";", "")
IF i = UBound(resultArray) THEN
Response.Write resultArray(i)
ELSE
Response.Write resultArray(i) & "; "
END IF
Next
If i remove the space after ; in split function it will also split "xyz:123" which i don't want to be.
Either i gave space after ; in split function (Line # 4) or not it shouldn't split the "xyz:123" which comes in double quotes.
Any suggestion how i can achieve this ?
Thanks in advance.
That suspiciously looks like a csv-file. You could try using ADO for this. Microsoft has a fairly extensive (and very useful) article on this: http://msdn.microsoft.com/en-us/library/ms974559.aspx
Since they describe exactly your problem, I think that just might be your solution. :)