472,353 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

string.replace question

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
5 2322
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) ...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant....
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. ...
11
by: billpaterson2006 | last post by:
I've been working on some code to search for specific textstrings and act upon them insome way. I've got the conversion sorted however there is 1...
3
by: MLH | last post by:
Back in mid-2003, lucason posted a question about removing punctuation chars from a string. Suggested code was posted using Replace function. Could...
10
by: TC | last post by:
Hey All, I have strings in currency. For example: "$1.29" What is the best way to convert them to decimal formatted like: "1.29" Thanks,...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.