By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,483 Members | 3,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,483 IT Pros & Developers. It's quick & easy.

string.replace question

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
"djc" <no***@nowhere.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...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.

Nov 21 '05 #2

P: n/a
Hi djc

For that sort of work the HtmlEncode\Decode method of the HttpServerUtility class may come in
handy.... depending whether you need a scapel or a sword.

http://msdn.microsoft.com/library/de...codeTopic1.asp

Richard
Nov 21 '05 #3

P: n/a
djc
thank you for the thorough information. I really appreciate it!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eR**************@TK2MSFTNGP10.phx.gbl...
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
"djc" <no***@nowhere.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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.


Nov 21 '05 #4

P: n/a
djc
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" <fa**@address.com> wrote in message
news:O0**************@tk2msftngp13.phx.gbl...
Hi djc

For that sort of work the HtmlEncode\Decode method of the HttpServerUtility class may come in handy.... depending whether you need a scapel or a sword.

http://msdn.microsoft.com/library/de...codeTopic1.asp
Richard

Nov 21 '05 #5

P: n/a
"djc" <no***@nowhere.com> wrote in message news:eU*************@TK2MSFTNGP11.phx.gbl...
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
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.