string.replace question

  • Thread starter Thread starter djc
  • Start date Start date
D

djc

I need to prepare a large text database field to display in an asp.net
repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and
it works fine. However, now I also want to be able to replace TAB's with
"&nbsp;"'s to preserve the user's indentation. My questions are:

1) even though I'll probably look it up before I get a reply.... whats the
vb code for TAB character?
2) more importantly; do I have to run this large field through 2 seperate
calls to the replace function? or is there a way to make it all happen with
one call to the replace function? or is there another funciton that can
handle this in one call?

Note: I know I can create my own function that I can call only once to
replace both sets of characters but I would have to call replace myself
twice. I'm less worried about actually calling it twice but more worried
about having to actually process the large string from beginning to end
twice. Is there some other function that can handle multiple replaces in one
shot?

any info is greatly appreciated. thanks.
 
Djc,
1) even though I'll probably look it up before I get a reply.... whats the
vb code for TAB character?
Have you looked at the ControlChars class? Specifically ControlChars.Tab?

2) more importantly; do I have to run this large field through 2 seperate
calls to the replace function?
You could use a RegEx to support a single call, however you need to supply a
callback (delegate) which indicates what the replaced text is suppose to be.

Something like:

Imports System.Text.RegularExpressions

Dim input As String = some really large string from the database...


Static regex As New Regex("\t|\r", RegexOptions.Compiled)
input = regex.Replace(input, AddressOf ReplaceWith)


Private Shared Function ReplaceWith(ByVal match As Match) As String
Select Case match.Value
Case ControlChars.Tab
Return "&nbsp;"
Case ControlChars.Cr
Return "<br/>"
End Select
End Function



Alternatively you could use StringBuilder.Replace:

Something like:

Dim input As String = some really large string from the database...

Dim sb As New System.Text.StringBuilder(input, input.Length * 3)
sb.Replace(ControlChars.Cr, "<br/>")
sb.Replace(ControlChars.Tab, "&nbsp;")
' other replacements that I might need...
input = sb.ToString()

"input.Length * 3" is a guestimate on how much total room the resultant
string might be, giving this guestimate might avoid reallocating the buffer
used internally to StringBuilder. Not giving the guestimate, in this case,
will ensure that the buffer needs to expand (as you are replacing a single
character with multiple characters). I would use profiling to refine the
guestimate if needed...


Of course you could chain String.Replace also.

Dim input As String = some really large string from the database...

input = input.Replace(ControlChars.Cr,
"<br/>").Replace(ControlChars.Tab, "&nbsp;")

Which I used would depend on how many replacements I was doing & if what I
was replacing were fixed values such as yours or actual patterns, such as
one or more white space characters. Plus any profiling results...

Hope this helps
Jay
 
Thanks for the reply. I appreciate the input.
I am probably missing something but this refers to encoding URLs... I could
also use this for my scenario?

Richard Myers said:
Hi djc

For that sort of work the HtmlEncode\Decode method of the
HttpServerUtility class may come in
 
djc said:
Thanks for the reply. I appreciate the input.
I am probably missing something but this refers to encoding URLs... I could
also use this for my scenario?

Hi Djc,

Maybe? The method is called HtmlEncode. There is another method on the same class called UrlEncode
which is specifically for Urls.

It depends how much fine grain/custom control you want. The example for the method HtmlEncode is
just for a text string not a Url.

I often use this method (HtmlEncode) in ASP.net to store user entered data as one means of securing
against sql injection attacks.

Richard
 
Back
Top