Lazarus
Lazarus

Reputation:

C# Best way to replace x repeated tokens by one token

If I have:

Some text



More text






Even more text

What is the more elegant way to obtain:

Some text

More text

Even more text

All with knowing the number of repeated tokens

Upvotes: 0

Views: 2089

Answers (9)

Binary Worrier
Binary Worrier

Reputation: 51711

Without Regexs (which make my head hurt)

string RemoveRepeated(string needle, string haystack)
{
    string doubleNeedle = needle + needle;

    while (haystack.IndexOf(doubleNeedle) >= 0)
        haystack = haystack.Replace(doubleNeedle, needle);
    return haystack;
}

With fewer memory allocations

string RemoveRepeated(string needle, string haystack)
{
    if (needle == null)
        throw new ArgumentNullException("needle");

    if (haystack == null)
        throw new ArgumentNullException("haystack");

    if (needle == string.Empty || haystack == string.Empty)
        return haystack;

    string doubleNeedle = needle + needle;
    var buffer = new StringBuilder(haystack);
    int originalLength;
    do
    {
        originalLength = buffer.Length;
        buffer.Replace(doubleNeedle, needle);
    } while (originalLength != buffer.Length);

    return buffer.ToString();
}

Initial checks are equally valid in the first version too

Upvotes: 0

KJ Saxena
KJ Saxena

Reputation: 21838

Use regular expressions. Match the entire string '\r\n' and replace with a single '\r\n'

The function you need:

pattern = "(\\r\\n)+";
Regex rgx = new Regex(pattern);

newString = rgx.Replace(oldString, "\r\n");

EDIT: Apologies for missing the + earlier

Upvotes: 3

Oliver
Oliver

Reputation: 45111

Just a few days ago, there was nearly the same question around here in SO. There was not a NewLine the problem, instead it where whitespaces.

There was also the one guys who prefers the Split, Join method and the other site using a regex. So Jon made a comparison between both and it came out that a compile regex was much faster.

But i just can't find this question again...

Upvotes: 0

Zanoni
Zanoni

Reputation: 30978

The fastest way:

Regex reg = new Regex(@"(\r\n)+");

string replacedString = reg.Replace("YOUR STRING TO BE REPLACED", Environment.NewLine);

Upvotes: 0

pavium
pavium

Reputation: 15118

If the \r\n means what it usually does, you're replacing successive blank lines with a single blank line.

I'm sure there are tools for that purpose. I wouldn't know about C#, though.

Upvotes: 0

Guffa
Guffa

Reputation: 700412

You can use a regular expression:

str = Regex.Replace(str, "(\r\n)+", "\r\n")

Another way could be to split on the line breaks removing empty entries, then join again:

str = String.Join("\r\n", str.Split(new string[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries);

Consider if you should use the string literal "\r\n" or the Environment.NewLine constant. That depends on where the data comes from.

Upvotes: 0

Sean
Sean

Reputation: 4470

The method to do so using regular expressions would be

string replaced = System.Text.RegularExpressions.Regex
    .Replace(input, @"(?:\r\n)+", "\r\n");

(The (?:...) syntax is a non-capturing group, which can be replaced with a capturing group (just (...)), but that is slightly less efficient and not more readable, IMO.)

Upvotes: 7

Simonw
Simonw

Reputation: 852

I don't know C# syntax, but just use a simple regex to replace (\r\n)+ with (\r\n)

Upvotes: 1

Bojan Resnik
Bojan Resnik

Reputation: 7378

Perhaps something like:

var result = string.Join("\r\n", s.Split(new[]{"\r\n"}, StringSplitOptions.RemoveEmptyEntries))

Upvotes: 6

Related Questions