Code in VC++.NET

I have a string that has html tags. I want to replace <BR> with \n, but not sure if <BR> will be uppercase, lowercase, or a mixture of both. The string replace function allows only one parameter for the replaced value. Is there a better way of doing this than checking 4 times? I will display the string in a button. Thanks.

Example:
string = one<BR>two<br>three

str =string->Replace ("<BR>", S"\n");
str =string->Replace ("<br>", S"\n");
str =string->Replace ("<Br>", S"\n");
str =string->Replace ("<bR>", S"\n");

or

str =string->ToUpper()->Replace ("<BR>", S"\n");
but I do not want to make the string all upper or lower case.

Dani AI

Generated

A short follow-up that pulls the useful bits from the thread and gives a more robust, production-ready approach.

As found (and as suggested), regular expressions are the simplest way to match tags regardless of case. For real HTML it is better to match variations like <br/>, <br />, <BR id="x">, etc., and to use the platform newline constant so GUI controls render correctly. A compact C++/CLI example that handles those cases and is fast when reused:

using namespace System;
using namespace System::Text::RegularExpressions;

String^ html = "one<BR>two<br/>three<Br id=\"x\">four";
Regex^ rx = gcnew Regex("<\\s*br\\b[^>]*>", RegexOptions::IgnoreCase | RegexOptions::Compiled);
html = rx->Replace(html, Environment::NewLine);

If a regex is unwanted or unavailable, the loop-with-IndexOf approach suggested by can be implemented without changing case and without doing multiple Replace calls. The following shows the idea using String::IndexOf with StringComparison::OrdinalIgnoreCase and a StringBuilder to assemble the result:

using namespace System;
using namespace System::Text;

String^ html = "one<BR>two<br>three";
String^ pattern = "<br>";
String^ repl = Environment::NewLine;

int pos = html->IndexOf(pattern, StringComparison::OrdinalIgnoreCase);
if (pos != -1) {
    StringBuilder^ sb = gcnew StringBuilder();
    int last = 0;
    while (pos != -1) {
        sb->Append(html->Substring(last, pos - last));
        sb->Append(repl);
        last = pos + pattern->Length;
        pos = html->IndexOf(pattern, last, StringComparison::OrdinalIgnoreCase);
    }
    sb->Append(html->Substring(last));
    html = sb->ToString();
}

Notes and cautions: use a compiled Regex when doing many replacements for better performance; prefer Environment::NewLine (not just "\n") for Windows controls; and if the input is real, complex HTML (scripts, comments, nested tags or attributes that must be preserved), a DOM-based parser is safer than regex. Also useful context: pointed out C-style case-insensitive compares for byte buffers if working with native strings.

Recommended Answers

All 4 Replies

I would probably make a duplicate copy of the string, convert it to all upper (or lower) case. Then instead of using the Replace method you show, find the position of the first occurrence of "<BR>" then replace it in both copies of the string. I don't have enough experience with VC++.NET to show an example. But that class probably has a find function that returns the starting position of a substring.

or you could write your own function that will do case-insensitive search and replaces.

Sugestion: Use regex, produces just about the fastest searches possible (you need a external library thought, I doubt it comes included in vc++ atleast I havent seen it, boost.org is one good source).

In C you can use stricmp() (or _stricmp() with the Microsoft compilers). Or you can write your own function, as Ancient Dragon suggested.

I got it using Regex.

Example:
string = one<BR>two<br>three

str =string->Replace ("<BR>", S"\n");
str =string->Replace ("<br>", S"\n");
str =string->Replace ("<Br>", S"\n");
str =string->Replace ("<bR>", S"\n");

Solution:
str = Regex::Replace (str, S"<[Bb][Rr]>", S"\n");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.