2

I'm aimed at speed, must be ultra fast.

        string s = something;
        for (int j = 0; j < s.Length; j++)
        {
            if (s[j] == 'ь')
                if(s.Length>(j+1))
                    if(s[j+1] != 'о')
                        s[j] = 'ъ';

It gives me an error Error "Property or indexer 'string.this[int]' cannot be assigned to -- it is read only"

How do I do it the fastest way?

John Black
  • 21
  • 3
  • Can you explain this process? What is your end goal? – Evan Mulawski Jan 19 '11 at 20:47
  • what possible reason could you have to do this? just curious. – VoodooChild Jan 19 '11 at 20:48
  • Looks like you've forgotten } at the end. I think the main idea is to auto-correct Russian words, because there are some weired rules about `ъ` & `ь` symbols :) – Elalfer Jan 19 '11 at 20:48
  • OCR often doesn't differ 'ъ' with 'ь' and that's fine. There's a rule in Russian that states "ь" only before "о". This should fix it, but it must be very fast. – John Black Jan 19 '11 at 20:51
  • 1
    The simple answer? You simply **can't** modify strings. If you need to, then whoever gives you the string must give it to you in a mutable format... if they don't, then there's no proper workaround for you. – user541686 Jan 19 '11 at 20:53
  • @John Black: Your code seems to be trying to change ь to ъ if it's *not* before an о. Isn't that the opposite of what you want? – Mark Byers Jan 19 '11 at 20:56

3 Answers3

3

There are at least two options:

  • Use a StringBuilder and keep track of the previous character.
  • You could just use a regular expression "ь(?!о)" or a simple string replacement of "ьо" depending on what your needs are (your question seems self-contradictory).

I tested the performance of a StringBuilder approach versus regular expressions and there is very little difference - at most a factor of 2:

Method              Iterations per second
StringBuilder                  153480.094
Regex (uncompiled)              90021.978
Regex (compiled)               136355.787
string.Replace                1427605.174

If performance is critical for you I would strongly recommend making some performance measurements before jumping to conclusions about what the fastest approach is.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

Fast way? Use a StringBuilder.

Fastest way? Always pass around a char* and a length instead of a string so you can modify the buffer in-place, but make sure you don't ever modify any string object.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • something tells me this might be a joke, is it? – VoodooChild Jan 19 '11 at 20:49
  • ... no, it's not. What makes you think it's a joke? – user541686 Jan 19 '11 at 20:49
  • what about string.toCharArray and then modifying that? – NG. Jan 19 '11 at 20:50
  • It's neither the preferred way (like `StringBuilder`) nor the fastest way (like `char*`); it's just another potential method with neither advantages nor disadvantages. Any reason you think `char[]` has any advantages to `StringBuilder` or `char*`? – user541686 Jan 19 '11 at 20:50
  • @Lambert - not really - just wondering. It might work for his algorithm, but I imagine your use of StringBuilder would call replace at the appropriate time? Agreed char* would always be the fastest but wondered if CharArray would be something that was a good compromise for anybody looking at C# code and crying at the site of a pointer. – NG. Jan 19 '11 at 21:29
  • @SB: `char[]` is totally fine, but it doesn't have any particularly special features (ease or power) so I didn't mention it. – user541686 Jan 19 '11 at 21:45
2

Strings in .Net is read-only. You could use StringBuilder.

Victor Haydin
  • 3,518
  • 2
  • 26
  • 41