473,385 Members | 1,570 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,385 software developers and data experts.

String Substitution - Performance

Anyone,

I am trying to do a string replace of a custom Html Tag that is Case
Insensitive and Fast, I will be calling this function a bunch of times.

Any thoughts about using maybe a StringBuilder or StringReader/Writer to
increase performance.

Please feel free to mark up my code and send it back. Thanks for any
help....

public string FormattedHtml
{
get
{
string formattedHtml = _html;
formattedHtml = ReplaceHtmlTag(formattedHtml, "heading", "h" +
_headingLevel.ToString());
return formattedHtml;
}
}
private string ReplaceHtmlTag(string source, string tag, string newTag)
{
string outHtml = source;
string startTag = "<" + tag.ToLowerInvariant();
string endTag = "</" + tag.ToLowerInvariant();
int pos = outHtml.ToLowerInvariant().IndexOf(startTag);
while (pos >= 0)
{
string begin = outHtml.Substring(0, pos);
outHtml = begin + "<" + newTag + outHtml.Substring(pos + startTag.Length,
outHtml.Length - pos - startTag.Length);
pos = outHtml.ToLowerInvariant().IndexOf(startTag, pos);
}
pos = outHtml.ToLowerInvariant().IndexOf(endTag);
while (pos >= 0)
{
string begin = outHtml.Substring(0, pos);
outHtml = begin + "</" + newTag + outHtml.Substring(pos + endTag.Length,
outHtml.Length - pos - endTag.Length);
pos = outHtml.ToLowerInvariant().IndexOf(endTag, pos);
}
return outHtml;
}
Apr 17 '06 #1
8 5457
Ben Dewey <As*********@hotmail.com> wrote:
I am trying to do a string replace of a custom Html Tag that is Case
Insensitive and Fast, I will be calling this function a bunch of times.

Any thoughts about using maybe a StringBuilder or StringReader/Writer to
increase performance.


Define "a bunch of times". Have you benchmarked it with real world data
and found it to be a significant bottlneck? If not, go with the
simplest, most readable code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 17 '06 #2
Well in that case. Is there an easy, readable String.Replace that is Case
Insensitive?
Apr 17 '06 #3
Ben Dewey <As*********@hotmail.com> wrote:
Well in that case. Is there an easy, readable String.Replace that is Case
Insensitive?


Not that I can see - but you could use IndexOf with an appropriate
StringOptions to do a case-insensitive lookup, rather than lower-casing
the whole string.

I suspect you may well find it just as easy to read a version which
uses StringBuilder as the version which doesn't, however. It's just a
case of finding the next start of the tag, appending the section from
the end of the last tag to that next index to the StringBuilder, then
appending the new tag, rinse and repeat. Just be careful with the edge
cases (i.e. make sure it works if there's a tag at the start, if
there's a tag at the end, and if there are two tags together).

Is there any chance you'll have a <tag /> kind of tag, rather than
<tag>stuff</tag>?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 17 '06 #4
How about this:

new Value = String.Replace(source.ToLower(), tag.ToLower(), newTag);

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Ben Dewey" <As*********@hotmail.com> wrote in message
news:eV**************@TK2MSFTNGP02.phx.gbl...
Well in that case. Is there an easy, readable String.Replace that is Case
Insensitive?

Apr 17 '06 #5
Thats no good, because it returns the whole output as lowercase.
Apr 17 '06 #6
If you plan to code against the xhtml 1.0 standard, then the markup is
required to be lower case.

Apr 17 '06 #7
SP

"Ben Dewey" <As*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Anyone,

I am trying to do a string replace of a custom Html Tag that is Case
Insensitive and Fast, I will be calling this function a bunch of times.

Any thoughts about using maybe a StringBuilder or StringReader/Writer to
increase performance.

Please feel free to mark up my code and send it back. Thanks for any
help....


An alternative is to perform the lower casing once and use this lower cased
string to find the changes that you need to make on the original source
string. The code below is a quick hack of this concept as I do the string
construction on both the lower case and the source so that the SubString
returns the correct value. I would also extract a method for the start tag
and end tag code as it is highly duplicated.

SP
string lowerC = source.ToLowerInvariant();

string startTag = "<" + tag.ToLowerInvariant();

string endTag = "</" + tag.ToLowerInvariant();

int pos = lowerC.IndexOf(startTag);

while(pos >= 0)

{

string begin = lowerC.Substring(0, pos);

lowerC = begin + "<" + newTag + lowerC.Substring(pos + startTag.Length,
lowerC.Length - pos - startTag.Length);

source = begin + "<" + newTag + source.Substring(pos + startTag.Length,
source.Length - pos - startTag.Length);

pos = lowerC.IndexOf(startTag, pos);

}

pos = lowerC.IndexOf(endTag);

while(pos >= 0)

{

string begin = lowerC.Substring(0, pos);

lowerC = begin + "</" + newTag + lowerC.Substring(pos + endTag.Length,
lowerC.Length - pos - endTag.Length);

source = begin + "</" + newTag + source.Substring(pos + endTag.Length,
source.Length - pos - endTag.Length);

pos = lowerC.IndexOf(endTag, pos);

}

return source;
Apr 17 '06 #8
Sorry, I meant:

new Value = source.ToLower().Replace( tag.ToLower(), newTag);

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Ben Dewey" <As*********@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
Thats no good, because it returns the whole output as lowercase.

Apr 18 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
hello! normally, if you are given binary data in a string (such as from a call to fread), you can call the strlen function on this string to get its size: $buffer = fread($file, 100000); if...
3
by: RiGGa | last post by:
Hi, I am trung to create a mysql query string that contais two variables, the first holds a table name and the second holds the values as a tuple. I have tried the following however I can not...
12
by: John Leslie | last post by:
I need to write a string to a file in EBCDIC. Do I need to do it character by character using a translation table, or is there a function to translate the whole string? (I am aware that I can...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
5
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
4
by: Cristian.Codorean | last post by:
I was just reading a "Python Speed/Performance Tips" article on the Python wiki http://wiki.python.org/moin/PythonSpeed/PerformanceTips and I got to the part that talks about string concatenation...
21
by: Hitesh | last post by:
Hi, I get path strings from a DB like: \\serverName\C:\FolderName1\FolderName2\example.exe I am writing a script that can give me access to that exe file. But problem is that string is not...
3
by: Skip | last post by:
OK, I'm a novice in JS but have lots of coding experience. I am trying to accomplish something that would seem somewhat simple - BUT IT'S NOT. I have a basic window that calls another window...
6
by: Generic Usenet Account | last post by:
I was extremely surprised to learn that the extremely rich C++ string API does not have even a single menthod devoted to string substitution i.e. given a string, replace all instances of pattern-1...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.