In VB, this seems to work fine:
Dim strlawl As String = "Hello" & vbCrLf & "Goodbye"
Dim lenlawl As Integer = strlawl.Length // = 14
strlawl = strlawl.replace(vbCrLf, vbLf)
lenlawl = strlawl.Length // = 13
I edited the initial change to use string functions, since they are much more efficient than regex, and finding a linebreak isn't exactly a "pattern". Though, with the string method, you wont be replacing multiple line-breaks with one, as you would be doing if you used 'regex.replace(strlawl, "[\r\n]+", vbLf)'
However, If you find the requirement to have both the line break and carriage return when you bring it back out, you'll want to use regex, because the inverse of the above string function would be very unstable. So:
strlawl = System.Text.RegularExpressions.Regex.Replace(strlawl, "[\r\n]+", Environment.Newline)
Yes, I do recommend keeping both the \r and \n in there like that for backward compatibility. On the other hand, it is more than likely not even necessary for you to re-convert. Line Feed should cause a break without the Carriage Return. But hey, clean code is clean.
Breakdown:
[ // Begins a new Character Class
\r // Allow matches for Carriage Return
\n // Also allow matches for Line Feed
]+ // Require at least one, but match as many as possible
Doing a regex replace is a bit heavy. It isn't a real concern, but if your replacement doesn't require pattern matching, it's a no-brainer to use string replacement. That being said, the Replace function is very efficient and isn't messy.
With the nature of mutliline textbox (or RTB's) being what they are, you're going to have \r\n in your return, and the only way I know to get rid of them is to replace them with something. My initial thought to answer the question was to replace them with a Pipe, then change them back when outputting, but I don't know what all of your textboxes accept or their purpose, so I went the safe route with the above response.
That being said, another option might be:
strlawl.replace(vbCr, Nothing)
But still, if you want both the CR and LF in your returned value, you'd be safest to use Regex to find patterns of any number or combination of consecutive CR's and/or LF's, and just properly replacing them with CRLF or NewLine.
All THAT being said, you say you have multiple pages and multiple text areas. Your web pages (codebehind) should not have any connections to your database. They should be using a data layer. A seperate DLL either in the project, or accessed via a Web Service. At a minimum, within that data layer you could have a shared method to reduce or reincorporate line breaks as you please. Then txtBox.Text = fix(strValue)
. You'd do this in the txtFieldName.DataBound
event.
ASP Codebehind:
Private Sub txtFieldName_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFieldName.DataBound
txtFieldName.Text = fix(strValue)
End Sub
Your question is about MVC Though, which I'm not quite pro at. But this is a link that would help. You could implement my answer here using tips from: Custom Model
Yeah, might be a bummer to have to update lots of text fields for one bug found. But that's the nature of the beast. In all honesty, I do hope somebody has another solution. But bugs are what they are.