1

NOTE: This is not a simple string replace question.

SomethingBlahBlah can be #SomeThingOtherthanThis

My case is as follows, I have a large string (<1024B, but >300B), that will have {#String} and {$String}.

To be more specific {#SomethingBlahBlah} and {$SomeOtherThingBlahBlah}, so in regexp {#w+} and {$w+}

My first question is, are regexps the only way to do it? I'd love a string replace solution or such, and secondly if it is, is there a way to just do one compiled regex and do a single pass through?

Can Linq help by any chance?

Community
  • 1
  • 1
user477470
  • 33
  • 3

5 Answers5

2

For a large string, and several different replacements, I would recommend using StringBuilder.

StringBuilder sb = new StringBuilder(input);
sb.Replace("{$String}", "Real Value");
sb.Replace("{$SomeOtherThingBlahBlah}", "Another Real Value");
return sb.ToString();

Manipulations will happen in memory, and a new string will not be allocated until you call ToString().

Matt Brunell
  • 10,141
  • 3
  • 34
  • 46
1

You can use one of these:

Option 1

Regex:

\{(?:#|\$)(\w+)}

Text:

{#SomethingBlahBlah} and {$SomeOtherThingBlahBlah}

Returns:

Result 1

   1. SomethingBlahBlah

Result 2

   1. SomeOtherThingBlahBlah

Option 2

Regex:

(\{(?:#|\$)(?:\w+)})

Text:

{#SomethingBlahBlah} and {$SomeOtherThingBlahBlah}

Returns:

Result 1

   1. {#SomethingBlahBlah}

Result 2

   1. {$SomeOtherThingBlahBlah}
tinifni
  • 2,372
  • 18
  • 15
  • 1
    No problem. You might want to investigate Matt Brunell's answer though. Don't forget to accept an answer once you have found one that suits you. Happy coding! – tinifni Oct 15 '10 at 23:44
1

IndexOf vs Regex: Tested using Stopwatch ticks over 100000 iterations with 500~ length string.

Method IndexOf

public static string Re(string str)
{
    int strSIndex = -1;
    int strEIndex = -1;

    strSIndex = str.IndexOf("{#");
    if (strSIndex == -1) strSIndex = str.IndexOf("{$");
    if (strSIndex == -1) return str;

    strEIndex = str.IndexOf("}");
    if (strEIndex == -1) return str;

    if (strEIndex < strSIndex)
    {
        strSIndex = str.IndexOf("{$");
        if (strSIndex == -1) return str;
    }

    str = str.Substring(0, strSIndex) + str.Substring(strEIndex + 1);

    return Re(str);
}

Regex Method

Regex re = new Regex(@"\{(?:#|\$)(\w+)}", RegexOptions.Compiled);
re.Replace(str, "");

Results (few replaces):

Fn: IndexOf
Ticks: 1181967

Fn: Regex
Ticks: 1482261

Notice that regex was set to compile before the iterations.

Results (lots of replaces):

Fn: Regex
Ticks: 19136772

Fn: IndexOf
Ticks: 37457111
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0
String.Replace("SomethingBlahBlah", "SomeOtherThingBlahBlah")

Edit: Just found a great answer by Jon Skeet in this thread.

Community
  • 1
  • 1
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • This would be a two-pass algorithm: once for replacing `{#Text}` and another for replacing `{$Text}`. The OP explicitly requested a one-pass solution. – Kirk Woll Oct 15 '10 at 21:36
  • I don't think "is there a way to just do one compiled regex and do a single pass through?" qualifies as an explicit request for a one-pass solution... – Dean Kuga Oct 15 '10 at 21:40
0

Regex takes more time to replace text instead of using String.Replace method. But Regex give you a huge power with text manipulation. LINQ hasn't a direct methods to work with string. It only can use existing features.

Yuriy
  • 2,670
  • 6
  • 33
  • 48