String - Optimization questions..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Zoury

    String - Optimization questions..

    Hi there! :O)

    I need to replace all the accentued character of a string by it's
    non-accentued-character equivalent.

    1. is there a way to do so without iterating trought the entire string and
    replacing character, with the Convert or Encoding class for example?
    2. if not, how can you actually replace a specific character in a string
    without recreating a new string?

    for now I do something like..
    //***
    string s = "aaaa ccaaa c aaa ca aaa";
    char[] chars = s.ToCharArray() ;
    for (int i = 0; i < chars.Length; i++)
    {
    if (chars[i] == 'c')
    {
    chars[i] = 'x';
    }
    s = new string(chars);
    }
    //***

    this is seems to be the lighter way we've found to do it... :O/

    I would done something like this but it's not possible instead but the
    compiler is complaining about the indexer being read-only..... (wich is
    poor..)
    //***
    string s = "aaaa ccaaa c aaa ca aaa";

    for (int i = 0; i < s.Lenght; i++)
    {
    if (s[i] == 'c')
    {
    s[i] = 'x';
    }
    }
    //***

    and I am **not** looking for the Replace() function...

    thanks for the help..

    --
    Best Regards
    Yanick Lefebvre

    Please posts answers to the group so all can benefit


  • Marina

    #2
    Re: String - Optimization questions..

    Why don't you want the Replace function? It was created so that you wouldn't
    have to write loops like you have there.

    And if you must use the loop, why are you recreated the string at the end of
    every loop? What is the point? You are always using just the last one, so
    recreate the string from the char array once the loop is finished, no reason
    to do it 20 times for 20 characters.


    "Zoury" <yanick_lefebvr e at hotmail dot com> wrote in message
    news:%23%23JqbU p5DHA.2696@TK2M SFTNGP09.phx.gb l...[color=blue]
    > Hi there! :O)
    >
    > I need to replace all the accentued character of a string by it's
    > non-accentued-character equivalent.
    >
    > 1. is there a way to do so without iterating trought the entire string and
    > replacing character, with the Convert or Encoding class for example?
    > 2. if not, how can you actually replace a specific character in a string
    > without recreating a new string?
    >
    > for now I do something like..
    > //***
    > string s = "aaaa ccaaa c aaa ca aaa";
    > char[] chars = s.ToCharArray() ;
    > for (int i = 0; i < chars.Length; i++)
    > {
    > if (chars[i] == 'c')
    > {
    > chars[i] = 'x';
    > }
    > s = new string(chars);
    > }
    > //***
    >
    > this is seems to be the lighter way we've found to do it... :O/
    >
    > I would done something like this but it's not possible instead but the
    > compiler is complaining about the indexer being read-only..... (wich is
    > poor..)
    > //***
    > string s = "aaaa ccaaa c aaa ca aaa";
    >
    > for (int i = 0; i < s.Lenght; i++)
    > {
    > if (s[i] == 'c')
    > {
    > s[i] = 'x';
    > }
    > }
    > //***
    >
    > and I am **not** looking for the Replace() function...
    >
    > thanks for the help..
    >
    > --
    > Best Regards
    > Yanick Lefebvre
    >
    > Please posts answers to the group so all can benefit
    >
    >[/color]


    Comment

    • Tu-Thach

      #3
      RE: String - Optimization questions..

      Zoury
      Strings are immutable. If you want to replace characters in a string, then a new string must be created

      Tu-Thac

      ----- Zoury wrote: ----

      Hi there! :O

      I need to replace all the accentued character of a string by it'
      non-accentued-character equivalent

      1. is there a way to do so without iterating trought the entire string an
      replacing character, with the Convert or Encoding class for example
      2. if not, how can you actually replace a specific character in a strin
      without recreating a new string

      for now I do something like.
      //**
      string s = "aaaa ccaaa c aaa ca aaa"
      char[] chars = s.ToCharArray()
      for (int i = 0; i < chars.Length; i++

      if (chars[i] == 'c'

      chars[i] = 'x'

      s = new string(chars)

      //**

      this is seems to be the lighter way we've found to do it... :O

      I would done something like this but it's not possible instead but th
      compiler is complaining about the indexer being read-only..... (wich i
      poor..
      //**
      string s = "aaaa ccaaa c aaa ca aaa"

      for (int i = 0; i < s.Lenght; i++

      if (s[i] == 'c'

      s[i] = 'x'


      //**

      and I am **not** looking for the Replace() function..

      thanks for the help.

      --
      Best Regard
      Yanick Lefebvr

      Please posts answers to the group so all can benefi



      Comment

      • Zoury

        #4
        Re: String - Optimization questions..

        > Strings are immutable. If you want to replace characters in a string,
        then a new string must be created.

        hhmmm... :O/
        ... thanks for the info.. :O)

        --
        Best Regards
        Yanick Lefebvre

        Please posts answers to the group so all can benefit


        Comment

        • Zoury

          #5
          Re: String - Optimization questions..

          Hi Marina! :O)
          [color=blue]
          > Why don't you want the Replace function? It was created so that you[/color]
          wouldn't[color=blue]
          > have to write loops like you have there.[/color]

          Because the sample shown wasn't really what is was trying to do... i don't
          konw yet how many characters i'll have to replace, so calling the replace
          function in a loop let say 50 times to change fifty differents accents
          sounded a little bit overhead to me..
          [color=blue]
          > And if you must use the loop, why are you recreated the string at the end[/color]
          of[color=blue]
          > every loop? What is the point? You are always using just the last one, so
          > recreate the string from the char array once the loop is finished, no[/color]
          reason[color=blue]
          > to do it 20 times for 20 characters.[/color]

          that's my mistakes.. sorry
          the actual code doesn't recreate it *in* the loop but really outside it :
          [color=blue][color=green]
          > > for now I do something like..
          > > //***
          > > string s = "aaaa ccaaa c aaa ca aaa";
          > > char[] chars = s.ToCharArray() ;
          > > for (int i = 0; i < chars.Length; i++)
          > > {
          > > if (chars[i] == 'c')
          > > {
          > > chars[i] = 'x';
          > > }
          > > }
          > > s = new string(chars);
          > > //***[/color][/color]

          there.. ;O)

          --
          Best Regards
          Yanick Lefebvre

          Please posts answers to the group so all can benefit


          Comment

          • C# Learner

            #6
            Re: String - Optimization questions..

            "Zoury" <yanick_lefebvr e at hotmail dot com> wrote:

            <snip>
            [color=blue]
            >I would done something like this but it's not possible instead but the
            >compiler is complaining about the indexer being read-only..... (wich is
            >poor..)
            >//***
            >string s = "aaaa ccaaa c aaa ca aaa";
            >
            >for (int i = 0; i < s.Lenght; i++)
            >{
            > if (s[i] == 'c')
            > {
            > s[i] = 'x';
            > }
            >}
            >//***[/color]

            This is because a string variable holds a reference, not a value.

            If the above was possible, this could happen:

            <code>

            1 string a, b;
            2
            3 a = "hello world";
            4 b = a;
            5
            6 a[0] = 'j';
            7
            8 Console.WriteLi ne(b); // this would output "jello world";

            </code>

            If this was allowed, the output would be "jello world" because in line
            4, b is assigned a's *reference*, not a's *value*.

            Sure, you can say a = "new string" and it won't affect what b
            references, because this will create a whole new string in a different
            memory location, and _a_ will now reference this new string, and _b_
            stays untouched.

            e.g.:

            <code>

            1 string a, b;
            2
            3 a = "hello world";
            4 b = a;
            5
            6 a = "new string";
            7
            8 Console.WriteLi ne(b); // this would output "hello world";

            </code>

            If you're going for optimization, I'd expect (though I'm not sure) a
            StringBuilder instance to be more efficient in this case.

            e.g.:

            <code>

            StringBuilder sb = new StringBuilder(s );

            for (int i = 0; i < sb.Length; ++i) {
            if (sb[i] == 'c') {
            sb[i] = 'x';
            }
            }

            s = StringBuilder.T oString();

            </code>

            Now, the above works, since a StringBuilder instance isn't immutable,
            whereas a String instance is.

            e.g.:

            <code>

            StringBuilder a, b;

            a = new StringBuilder(" hello world");
            b = a;

            a[0] = 'j';

            Console.WriteLi ne(b); // outputs "jello world"

            </code>

            This is perfectly valid.

            StringBuilder can be thought of as a replacement for String (string)
            for when you need mutability (the ability to change the value the
            string is referencing).

            <snip>

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: String - Optimization questions..

              <"Zoury" <yanick_lefebvr e at hotmail dot com>> wrote:[color=blue]
              > I need to replace all the accentued character of a string by it's
              > non-accentued-character equivalent.
              >
              > 1. is there a way to do so without iterating trought the entire string and
              > replacing character, with the Convert or Encoding class for example?[/color]

              No. Strings are immutable.
              [color=blue]
              > 2. if not, how can you actually replace a specific character in a string
              > without recreating a new string?[/color]

              No - see the above.
              [color=blue]
              > for now I do something like..
              > //***
              > string s = "aaaa ccaaa c aaa ca aaa";
              > char[] chars = s.ToCharArray() ;
              > for (int i = 0; i < chars.Length; i++)
              > {
              > if (chars[i] == 'c')
              > {
              > chars[i] = 'x';
              > }
              > s = new string(chars);
              > }[/color]

              And that's the best you can do - although you might want to check
              whether or not you've got anything to replace before you bother
              creating a copy.
              [color=blue]
              > this is seems to be the lighter way we've found to do it... :O/
              >
              > I would done something like this but it's not possible instead but the
              > compiler is complaining about the indexer being read-only..... (wich is
              > poor..)[/color]

              It's not poor at all - the alternative is for strings to be mutable,
              which would be *much* worse, IMO. You'd need to make a copy every time
              you wanted to pass a string into a method without risking have it
              changed, etc.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Martin Maat [EBL]

                #8
                Re: String - Optimization questions..

                > > Strings are immutable. If you want to replace characters in a string,[color=blue][color=green]
                > > then a new string must be created.[/color][/color]
                [color=blue]
                > hhmmm... :O/
                > .. thanks for the info.. :O)[/color]

                Strings are immutable indeed, but that is what StringBuilder is for. It
                allows the internal tinkering you want through a Replace method and a Chars
                collection.

                I don't see the point in sinking that low though, the risks and the trouble
                do not seem to be worth the supposed performance gain. I would try
                System.Text.Enc oding, it seems perfect for the job and I seriously doubt you
                can do better than the writers of that class.

                Martin.


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: String - Optimization questions..

                  Martin Maat [EBL] <dummy@somewher e.nl> wrote:[color=blue]
                  > I don't see the point in sinking that low though, the risks and the trouble
                  > do not seem to be worth the supposed performance gain. I would try
                  > System.Text.Enc oding, it seems perfect for the job and I seriously doubt you
                  > can do better than the writers of that class.[/color]

                  No, System.Text.Enc oding is for something entirely different -
                  conversions between characters and bytes. The OP never mentioned
                  anything to do with a byte-encoded version of the text.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Zoury

                    #10
                    Re: String - Optimization questions..

                    > Strings are immutable indeed, but that is what StringBuilder is for. It[color=blue]
                    > allows the internal tinkering you want through a Replace method and a[/color]
                    Chars[color=blue]
                    > collection.[/color]

                    Naaaa... thanks for the info! ;O)
                    [color=blue]
                    > I would try
                    > System.Text.Enc oding, it seems perfect for the job and I seriously doubt[/color]
                    you[color=blue]
                    > can do better than the writers of that class.[/color]

                    *no* doubt about it.. <g>
                    but do you have a clue on how to do it using this class?

                    --
                    Best Regards
                    Yanick Lefebvre

                    Please posts answers to the group so all can benefit


                    Comment

                    • Zoury

                      #11
                      Re: String - Optimization questions..

                      Thanks to all of you!

                      --
                      Best Regards
                      Yanick Lefebvre

                      Please posts answers to the group so all can benefit


                      Comment

                      • Zoury

                        #12
                        Re: String - Optimization questions..

                        Hi Jon! :O)
                        [color=blue]
                        > It's not poor at all - the alternative is for strings to be mutable,
                        > which would be *much* worse, IMO. You'd need to make a copy every time
                        > you wanted to pass a string into a method without risking have it
                        > changed, etc.[/color]

                        Now that I know about the StringBuilder class, I have to agree with you..
                        they did provide both ways to work with strings, wich is just plain
                        perfect... ;O)

                        --
                        Best Regards
                        Yanick Lefebvre

                        Please posts answers to the group so all can benefit


                        Comment

                        • Zoury

                          #13
                          Re: String - Optimization questions..

                          ok thanks!

                          --
                          Best Regards
                          Yanick Lefebvre

                          Please posts answers to the group so all can benefit


                          Comment

                          Working...