string problem

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

    #1

    string problem

    string s("0101");

    I can't change it into s=1101
    by

    s[0]='1';

    because string.this[i] is read-only, no assignment is allowed :-(

    thank you for your help

  • Peter Duniho

    #2
    Re: string problem

    "Mosquito Man" <modermjersay@y ahoo.comwrote in message
    news:1163836241 .928244.210640@ k70g2000cwa.goo glegroups.com.. .
    string s("0101");
    >
    I can't change it into s=1101
    by
    >
    s[0]='1';
    >
    because string.this[i] is read-only, no assignment is allowed :-(

    Perhaps the StringBuilder class is more suitable for your needs.


    Comment

    • Dave Sexton

      #3
      Re: string problem

      Hi,

      System.String is immutable. A better choice would be to use a StringBuilder:

      StringBuilder s = new StringBuilder(" 0101");
      s[0] = '1';

      --
      Dave Sexton

      "Mosquito Man" <modermjersay@y ahoo.comwrote in message
      news:1163836241 .928244.210640@ k70g2000cwa.goo glegroups.com.. .
      string s("0101");
      >
      I can't change it into s=1101
      by
      >
      s[0]='1';
      >
      because string.this[i] is read-only, no assignment is allowed :-(
      >
      thank you for your help
      >

      Comment

      • Truong Hong Thi

        #4
        Re: string problem

        Hi,

        If you want to change the string that way, you'd better use
        StringBuilder class.

        string s = "0101";
        StringBuilder builder = new StringBuilder(s );
        builder [0] = '1';
        builder [3] = '0';
        s = builder.ToStrin g();

        An alternative is use String.ToCharAr ray. You than can set each char to
        whatever you want, then create a new string, past the array to string
        constructor.

        string s = "0101";
        char[] chars = s.ToCharArray() ;
        chars[0] = '1';
        chars[3] = '0';
        s = new String(chars);

        Thi
        Thoughts and ideas about software development


        Mosquito Man wrote:
        string s("0101");
        >
        I can't change it into s=1101
        by
        >
        s[0]='1';
        >
        because string.this[i] is read-only, no assignment is allowed :-(
        >
        thank you for your help

        Comment

        • Mark R. Dawson

          #5
          RE: string problem

          Hi,
          strings are immutable, so you cannot change the contents of a string once
          it has been defined. If you want to change values you are going to have to
          use some combination of .SubString .Replace etc to replace individual values.

          HTH
          Mark.
          --



          "Mosquito Man" wrote:
          string s("0101");
          >
          I can't change it into s=1101
          by
          >
          s[0]='1';
          >
          because string.this[i] is read-only, no assignment is allowed :-(
          >
          thank you for your help
          >
          >

          Comment

          • Jon Shemitz

            #6
            Re: string problem

            Truong Hong Thi wrote:
            If you want to change the string that way, you'd better use
            StringBuilder class.
            An alternative is use String.ToCharAr ray. You than can set each char to
            whatever you want, then create a new string, past the array to string
            constructor.
            >
            string s = "0101";
            char[] chars = s.ToCharArray() ;
            chars[0] = '1';
            chars[3] = '0';
            s = new String(chars);
            Of course, this latter is exactly what the StringBuilder class is
            doing. All you gain by taking the manual approach is saving a very few
            cycles by not creating a StringBuilder instance ... at the cost of
            bigger, more complex source code that is harder to maintain.

            --


            What you need to know.

            Comment

            • rossum

              #7
              Re: string problem

              On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <modermjersay@y ahoo.com>
              wrote:
              >string s("0101");
              >
              >I can't change it into s=1101
              >by
              >
              >s[0]='1';
              >
              >because string.this[i] is read-only, no assignment is allowed :-(
              >
              >thank you for your help
              Others have pointed you to the StringBuilder class, which is probably
              the solution you are looking for.

              An alternative is to drop into unsafe code and use pointers which do
              allow you to change otherwise "immutable" strings:

              unsafe void OverwriteChar(s tring text, int pos, char newChar) {
              fixed (char* cPointer = text) {
              cPointer[pos] = newChar;
              }
              }

              I use this technique to overwrite security sensitive strings before
              releasing them for garbage collection.

              rossum

              Comment

              • Willy Denoyette [MVP]

                #8
                Re: string problem

                "rossum" <rossum48@coldm ail.comwrote in message
                news:8bsul2pj9v 4hnuf8e19o1gn37 bga0b6jqm@4ax.c om...
                On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <modermjersay@y ahoo.com>
                wrote:
                >
                >>string s("0101");
                >>
                >>I can't change it into s=1101
                >>by
                >>
                >>s[0]='1';
                >>
                >>because string.this[i] is read-only, no assignment is allowed :-(
                >>
                >>thank you for your help
                Others have pointed you to the StringBuilder class, which is probably
                the solution you are looking for.
                >
                An alternative is to drop into unsafe code and use pointers which do
                allow you to change otherwise "immutable" strings:
                >
                unsafe void OverwriteChar(s tring text, int pos, char newChar) {
                fixed (char* cPointer = text) {
                cPointer[pos] = newChar;
                }
                }
                >
                I use this technique to overwrite security sensitive strings before
                releasing them for garbage collection.
                >
                rossum
                >

                V2 of the framework provides a SecureString (System.Securit y) which has the advantage to be
                encrypted when live and deleted from memory when detached, no need for unsafe code.
                Willy.

                Comment

                • rossum

                  #9
                  Re: string problem

                  On Sat, 18 Nov 2006 22:11:43 +0100, "Willy Denoyette [MVP]"
                  <willy.denoyett e@telenet.bewro te:
                  >"rossum" <rossum48@coldm ail.comwrote in message
                  >news:8bsul2pj9 v4hnuf8e19o1gn3 7bga0b6jqm@4ax. com...
                  >On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <modermjersay@y ahoo.com>
                  >wrote:
                  >>
                  >>>string s("0101");
                  >>>
                  >>>I can't change it into s=1101
                  >>>by
                  >>>
                  >>>s[0]='1';
                  >>>
                  >>>because string.this[i] is read-only, no assignment is allowed :-(
                  >>>
                  >>>thank you for your help
                  >Others have pointed you to the StringBuilder class, which is probably
                  >the solution you are looking for.
                  >>
                  >An alternative is to drop into unsafe code and use pointers which do
                  >allow you to change otherwise "immutable" strings:
                  >>
                  > unsafe void OverwriteChar(s tring text, int pos, char newChar) {
                  > fixed (char* cPointer = text) {
                  >cPointer[pos] = newChar;
                  > }
                  > }
                  >>
                  >I use this technique to overwrite security sensitive strings before
                  >releasing them for garbage collection.
                  >>
                  >rossum
                  >>
                  >
                  >
                  >V2 of the framework provides a SecureString (System.Securit y) which has the advantage to be
                  >encrypted when live and deleted from memory when detached, no need for unsafe code.
                  >Willy.
                  Thanks for that, I used the technique in V1 and haven't needed it
                  since the move to V2.

                  rossum

                  Comment

                  • Jon Skeet [C# MVP]

                    #10
                    Re: string problem

                    rossum <rossum48@coldm ail.comwrote:
                    On 17 Nov 2006 23:50:42 -0800, "Mosquito Man" <modermjersay@y ahoo.com>
                    wrote:
                    >
                    string s("0101");

                    I can't change it into s=1101
                    by

                    s[0]='1';

                    because string.this[i] is read-only, no assignment is allowed :-(

                    thank you for your help
                    Others have pointed you to the StringBuilder class, which is probably
                    the solution you are looking for.
                    >
                    An alternative is to drop into unsafe code and use pointers which do
                    allow you to change otherwise "immutable" strings:
                    >
                    unsafe void OverwriteChar(s tring text, int pos, char newChar) {
                    fixed (char* cPointer = text) {
                    cPointer[pos] = newChar;
                    }
                    }
                    >
                    I use this technique to overwrite security sensitive strings before
                    releasing them for garbage collection.
                    Note that that can produce *very* disturbing results - in this case, if
                    the literal "0101" were changed, *all* string literals in the code
                    which should be "0101" would then be "1101".

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                    If replying to the group, please do not mail me too

                    Comment

                    Working...