String again

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

    #1

    String again

    Hello,
    I want to change one character in a string to a new character. How can i
    do this. For example I have a string called x and it contains "This is a
    test" and I want to change the i in is to a so after change it become "This
    as a test". I tried the following code without any sucess:

    public void ChangeChar(int index,char NewChar)

    {

    m_MyString.Remo ve(Index);

    m_Privilage.Ins ert(Index,NewCh ar);

    }

    How can I convert a char to a string? How can I convert a char to char *?



    Regards




  • zacks@construction-imaging.com

    #2
    Re: String again

    On Oct 2, 10:47 am, "ma" <m...@nowhere.c omwrote:
    Hello,
    I want to change one character in a string to a new character. How can i
    do this. For example I have a string called x and it contains "This is a
    test" and I want to change the i in is to a so after change it become "This
    as a test". I tried the following code without any sucess:
    >
    public void ChangeChar(int index,char NewChar)
    >
    {
    >
    m_MyString.Remo ve(Index);
    >
    m_Privilage.Ins ert(Index,NewCh ar);
    >
    }
    >
    How can I convert a char to a string? How can I convert a char to char *?
    >
    Regards
    I take it you missed the String.Replace method when you searched the
    help text?

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: String again

      ma,

      You will have to return the new string. Strings are immutable, so you
      can't change the contents (at least, not without bending over backwards).
      You really want to do this:

      public void ChangeChar(int index, char newChar)
      {
      // Remove the character first.
      m_MyString = m_MyString.Remo ve(index);

      // Insert the character.
      m_MyString = m_MyString.Inse rt(index, newChar);
      }

      I changed the code so that you are changing the same variable. You were
      not doing that in your original posted code.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "ma" <ma@nowhere.com wrote in message
      news:O8$4iMQBIH A.4200@TK2MSFTN GP04.phx.gbl...
      Hello,
      I want to change one character in a string to a new character. How can
      i do this. For example I have a string called x and it contains "This is a
      test" and I want to change the i in is to a so after change it become
      "This as a test". I tried the following code without any sucess:
      >
      public void ChangeChar(int index,char NewChar)
      >
      {
      >
      m_MyString.Remo ve(Index);
      >
      m_Privilage.Ins ert(Index,NewCh ar);
      >
      }
      >
      How can I convert a char to a string? How can I convert a char to char *?
      >
      >
      >
      Regards
      >
      >
      >
      >

      Comment

      • Hans Kesting

        #4
        Re: String again

        ma used his keyboard to write :
        Hello,
        I want to change one character in a string to a new character. How can i
        do this. For example I have a string called x and it contains "This is a
        test" and I want to change the i in is to a so after change it become "This
        as a test". I tried the following code without any sucess:
        >
        public void ChangeChar(int index,char NewChar)
        >
        {
        >
        m_MyString.Remo ve(Index);
        >
        m_Privilage.Ins ert(Index,NewCh ar);
        >
        }
        >
        How can I convert a char to a string? How can I convert a char to char *?
        >
        >
        >
        Regards
        As mentioned before, you can not change the contents of a String. You
        can however build a new string and replace the previous reference.
        So build a new string: part before the insert, the new char, part after
        the insert.

        You might also want to look into StringBuilder (namespace System.Text),
        which does have an Insert method.

        Hans Kesting


        Comment

        • ma

          #5
          Re: String again

          Thanks,
          But the code doesn't compile with error code that it can not convert char to
          string.
          How can I change char to string? Note that it is not char * but char.

          Regards




          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
          message news:u$L5IWQBIH A.1168@TK2MSFTN GP02.phx.gbl...
          ma,
          >
          You will have to return the new string. Strings are immutable, so you
          can't change the contents (at least, not without bending over backwards).
          You really want to do this:
          >
          public void ChangeChar(int index, char newChar)
          {
          // Remove the character first.
          m_MyString = m_MyString.Remo ve(index);
          >
          // Insert the character.
          m_MyString = m_MyString.Inse rt(index, newChar);
          }
          >
          I changed the code so that you are changing the same variable. You
          were not doing that in your original posted code.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "ma" <ma@nowhere.com wrote in message
          news:O8$4iMQBIH A.4200@TK2MSFTN GP04.phx.gbl...
          >Hello,
          > I want to change one character in a string to a new character. How can
          >i do this. For example I have a string called x and it contains "This is
          >a test" and I want to change the i in is to a so after change it become
          >"This as a test". I tried the following code without any sucess:
          >>
          >public void ChangeChar(int index,char NewChar)
          >>
          >{
          >>
          >m_MyString.Rem ove(Index);
          >>
          >m_Privilage.In sert(Index,NewC har);
          >>
          >}
          >>
          >How can I convert a char to a string? How can I convert a char to char *?
          >>
          >>
          >>
          >Regards
          >>
          >>
          >>
          >>
          >
          >

          Comment

          • ma

            #6
            Re: String again


            <zacks@construc tion-imaging.comwrot e in message
            news:1191337725 .369451.28600@2 2g2000hsm.googl egroups.com...
            On Oct 2, 10:47 am, "ma" <m...@nowhere.c omwrote:
            >Hello,
            > I want to change one character in a string to a new character. How
            >can i
            >do this. For example I have a string called x and it contains "This is a
            >test" and I want to change the i in is to a so after change it become
            >"This
            >as a test". I tried the following code without any sucess:
            >>
            >public void ChangeChar(int index,char NewChar)
            >>
            >{
            >>
            >m_MyString.Rem ove(Index);
            >>
            >m_Privilage.In sert(Index,NewC har);
            >>
            >}
            >>
            >How can I convert a char to a string? How can I convert a char to char *?
            >>
            >Regards
            >
            I take it you missed the String.Replace method when you searched the
            help text?
            >
            Thanks.
            Replace change all occurrence of the char with new ones but I want to
            replace one specific one with a new one.

            Regards



            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: String again

              You can call ToString on the character, like so:


              public void ChangeChar(int index, char newChar)
              {
              // Remove the character first.
              m_MyString = m_MyString.Remo ve(index);

              // Insert the character.
              m_MyString = m_MyString.Inse rt(index, newChar.ToStrin g());
              }


              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omwrote in
              message news:u$L5IWQBIH A.1168@TK2MSFTN GP02.phx.gbl...
              ma,
              >
              You will have to return the new string. Strings are immutable, so you
              can't change the contents (at least, not without bending over backwards).
              You really want to do this:
              >
              public void ChangeChar(int index, char newChar)
              {
              // Remove the character first.
              m_MyString = m_MyString.Remo ve(index);
              >
              // Insert the character.
              m_MyString = m_MyString.Inse rt(index, newChar);
              }
              >
              I changed the code so that you are changing the same variable. You
              were not doing that in your original posted code.
              >
              >
              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m
              >
              "ma" <ma@nowhere.com wrote in message
              news:O8$4iMQBIH A.4200@TK2MSFTN GP04.phx.gbl...
              >Hello,
              > I want to change one character in a string to a new character. How can
              >i do this. For example I have a string called x and it contains "This is
              >a test" and I want to change the i in is to a so after change it become
              >"This as a test". I tried the following code without any sucess:
              >>
              >public void ChangeChar(int index,char NewChar)
              >>
              >{
              >>
              >m_MyString.Rem ove(Index);
              >>
              >m_Privilage.In sert(Index,NewC har);
              >>
              >}
              >>
              >How can I convert a char to a string? How can I convert a char to char *?
              >>
              >>
              >>
              >Regards
              >>
              >>
              >>
              >>
              >
              >

              Comment

              • Ben Voigt [C++ MVP]

                #8
                Re: String again


                "ma" <ma@nowhere.com wrote in message
                news:O8$4iMQBIH A.4200@TK2MSFTN GP04.phx.gbl...
                Hello,
                I want to change one character in a string to a new character. How can
                i do this. For example I have a string called x and it contains "This is a
                test" and I want to change the i in is to a so after change it become
                "This as a test". I tried the following code without any sucess:
                >
                public void ChangeChar(int index,char NewChar)
                >
                {
                >
                m_MyString.Remo ve(Index);
                >
                m_Privilage.Ins ert(Index,NewCh ar);
                >
                }
                >
                How can I convert a char to a string? How can I convert a char to char *?
                See string.ToCharAr ray(), once you have an array you can overwrite any
                character at will.


                Comment

                • bob

                  #9
                  Re: String again


                  Hi ma,
                  If your string varies then perhaps you should be using regular
                  expressions to find and replace. See regex class
                  regards
                  Bob

                  On Tue, 2 Oct 2007 15:47:34 +0100, "ma" <ma@nowhere.com wrote:
                  >Hello,
                  I want to change one character in a string to a new character. How can i
                  >do this. For example I have a string called x and it contains "This is a
                  >test" and I want to change the i in is to a so after change it become "This
                  >as a test". I tried the following code without any sucess:
                  >
                  >public void ChangeChar(int index,char NewChar)
                  >
                  >{
                  >
                  >m_MyString.Rem ove(Index);
                  >
                  >m_Privilage.In sert(Index,NewC har);
                  >
                  >}
                  >
                  >How can I convert a char to a string? How can I convert a char to char *?
                  >
                  >
                  >
                  >Regards
                  >
                  >
                  >

                  Comment

                  • Jesse Houwing

                    #10
                    Re: String again

                    Hello Bob,
                    Hi ma,
                    If your string varies then perhaps you should be using regular
                    expressions to find and replace. See regex class
                    regards
                    Bob
                    On Tue, 2 Oct 2007 15:47:34 +0100, "ma" <ma@nowhere.com wrote:
                    >
                    >Hello,
                    >I want to change one character in a string to a new character. How
                    >can i
                    >do this. For example I have a string called x and it contains "This
                    >is a
                    >test" and I want to change the i in is to a so after change it become
                    >"This
                    >as a test". I tried the following code without any sucess:
                    >public void ChangeChar(int index,char NewChar)
                    >>
                    >{
                    >>
                    >m_MyString.Rem ove(Index);
                    >>
                    >m_Privilage.In sert(Index,NewC har);
                    >>
                    >}
                    >>
                    >How can I convert a char to a string? How can I convert a char to
                    >char *?
                    As a string is an immutable type in .NEt you'd have to change your code a
                    bit:

                    public string ChangeChar(int index,char NewChar)
                    {
                    return m_MyString.Remo ve(Index).Inser t(Index,NewChar );
                    }


                    Or you might be able to use a ref parameter:

                    public void ChangeChar(ref string input, int index,char NewChar)
                    {
                    input = input.Remove(In dex).Insert(Ind ex,NewChar);
                    }

                    Also consider that if you're going to search and replace many characters
                    in your string, it might be much faster to do all the manipulations on a
                    StringBuilder instead.
                    --
                    Jesse Houwing
                    jesse.houwing at sogeti.nl


                    Comment

                    Working...