473,378 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 2407
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 text: text = text.replace( searchTerm, 'other' ) ...
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 the characters </ in sequence. So how am I...
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) or would you have to do something like: if...
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 to highlight certain words within the text of the...
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. PopupLink = "javascript:PopUpWindow(" &...
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. Typically if I create a string in a loop I always use a...
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 problem remaining. I am trying to work out how...
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 the FN below be easily modified for use with A97...
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, TC
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.