Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. character postion replacemenet

character postion replacemenet

Scheduled Pinned Locked Moved C#
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    annie_bel
    wrote on last edited by
    #1

    hello! I have a following string.

    string str="flower pot";

    In above string i want to replace the position of the character.

    eg: "o" replace with its just next character.

    my new string is shown like:

    "flwoer pto"

    thanx

    G 1 Reply Last reply
    0
    • A annie_bel

      hello! I have a following string.

      string str="flower pot";

      In above string i want to replace the position of the character.

      eg: "o" replace with its just next character.

      my new string is shown like:

      "flwoer pto"

      thanx

      G Offline
      G Offline
      Gonzalo Cao
      wrote on last edited by
      #2

      public void RelocateChar(ref string text, int pos1, int pos2)
      {
      char[] supportArray = text.ToCharArray();
      char supportChar;

      supportChar = supportArray[pos2];
      supportArray[pos2] = supportArray[pos1];
      supportArray[pos1] = supportChar;
      text = new string(supportArray);
      }

      Maybe there are better solutions, but this one should work.

      P 1 Reply Last reply
      0
      • G Gonzalo Cao

        public void RelocateChar(ref string text, int pos1, int pos2)
        {
        char[] supportArray = text.ToCharArray();
        char supportChar;

        supportChar = supportArray[pos2];
        supportArray[pos2] = supportArray[pos1];
        supportArray[pos1] = supportChar;
        text = new string(supportArray);
        }

        Maybe there are better solutions, but this one should work.

        P Offline
        P Offline
        ps_prakash02
        wrote on last edited by
        #3

        Have a look through this code and see whether it solves your problem. private void button1_Click(object sender, EventArgs e) { string str = "flower pot"; MessageBox.Show("The source string " + str + System.Environment.NewLine + System.Environment.NewLine + "The Replace Content " + SwapCharacters(str, 'o')); } public string SwapCharacters(string strSrc, char chrSrc) { string strDst = string.Empty; try { while (strSrc.IndexOf(chrSrc) > -1) { strDst += strSrc.Substring(0, strSrc.IndexOf(chrSrc)); strDst += strSrc.Substring(strSrc.IndexOf(chrSrc) + 1, 1); strDst += chrSrc; strSrc = strSrc.Substring(strSrc.IndexOf(chrSrc) + 2); } strDst += strSrc; } catch (Exception ex) { throw ex; } return strDst; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups