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. How to replace very first character only......?

How to replace very first character only......?

Scheduled Pinned Locked Moved C#
regextutorialquestion
14 Posts 7 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.
  • P Pankaj Joshi

    I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

    Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

    K Offline
    K Offline
    KennyPatel
    wrote on last edited by
    #5

    Hi. I hope this work for you. Write this in your some click events. string xNew=""; int j = 1; for (int i = 0; i < TextBox1.Text.Length; i++) { string x = TextBox1.Text.Substring(i, 1); if (x == "-") { if (j == 1) { xNew = xNew + "."; j++; } else { xNew = xNew + x; } } else { xNew = xNew + x; } } Label1.Text = xNew; Small Logic. Thank you.

    "Good Thing Goes With Good People..."

    1 Reply Last reply
    0
    • P Pankaj Joshi

      I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

      Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

      K Offline
      K Offline
      KennyPatel
      wrote on last edited by
      #6

      Hi. I hope this will help you out.. Write somewhere in button event and test it.... string xNew=""; int j = 1; for (int i = 0; i < TextBox1.Text.Length; i++) { string x = TextBox1.Text.Substring(i, 1); if (x == "-") { if (j == 1) { xNew = xNew + "."; j++; } else { xNew = xNew + x; } } else { xNew = xNew + x; } } Label1.Text = xNew; Thanks for watching it....

      "Good Thing Goes With Good People..."

      1 Reply Last reply
      0
      • P Pankaj Joshi

        I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

        Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

        K Offline
        K Offline
        KennyPatel
        wrote on last edited by
        #7

        Hi, Will Help you lot..... sure string xNew=""; int j = 1; for (int i = 0; i < TextBox1.Text.Length; i++) { string x = TextBox1.Text.Substring(i, 1); if (x == "-") { if (j == 1) { xNew = xNew + "."; j++; } else { xNew = xNew + x; } } else { xNew = xNew + x; } } Label1.Text = xNew;

        "Good Thing Goes With Good People..."

        1 Reply Last reply
        0
        • P Pankaj Joshi

          I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

          Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

          K Offline
          K Offline
          KennyPatel
          wrote on last edited by
          #8

          :laugh:Hi. //Hope this will help you to a Lot for sure..... string xNew=""; int j = 1; for (int i = 0; i < TextBox1.Text.Length; i++) { string x = TextBox1.Text.Substring(i, 1); if (x == "-") { if (j == 1) { xNew = xNew + "."; j++; } else { xNew = xNew + x; } } else { xNew = xNew + x; } } Label1.Text = xNew; }

          "Good Thing Goes With Good People..."

          1 Reply Last reply
          0
          • A Andrei Ungureanu

            string str = "ABC-X-Y-Z";
            int index = str.IndexOf("-");
            str = String.Format("{0}{1}{2}", str.Substring(0, index), ".", str.Substring(index + 1, str.Length - index - 1));

            Hope it helps.

            I will use Google before asking dumb questions

            P Offline
            P Offline
            Pankaj Joshi
            wrote on last edited by
            #9

            Thanks for your reply. But it will give error in case of string str = ""; Because this input is depend upon the end user So I can-not say the input data in Correct format. Or if dash is not found then it will also give the error. I make new code please let me know this is Ok or not...? ************************************************************** public static string getNewItemName(string actualItemName, string saparaterCharacter) { string valueToReturn = ""; try { int ipos = actualItemName.ToString().IndexOf(saparaterCharacter.ToString()); if (ipos > -1) //That means saparator is not found... { if (saparaterCharacter.ToString().Length == 0) valueToReturn = actualItemName.ToString(); else { string strFirstPart = actualItemName.ToString().Substring(0, ipos).ToString(); string strSecondPart = ""; if (actualItemName.ToString().Length == 0) strSecondPart = actualItemName.ToString().Substring(ipos).ToString(); else strSecondPart = actualItemName.ToString().Substring(ipos + 1).ToString(); valueToReturn = strFirstPart + ":" + strSecondPart; } } else valueToReturn = actualItemName.ToString(); } catch (Exception) { //In Case of any error we are returning the same name valueToReturn = actualItemName.ToString(); } return valueToReturn; } **************************************************************

            Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

            1 Reply Last reply
            0
            • S Sun Rays

              Hi, u can try this. string str= "xyz-x-y-z"; int i = str.IndexOf("-"); str = str.Insert(i+1, ".").Remove(i, 1);

              Thanks, Sun Rays To get something you must have to try once. My Articles

              P Offline
              P Offline
              Pankaj Joshi
              wrote on last edited by
              #10

              Thanks for your reply. But it will give error in case of string str = ""; Because this input is depend upon the end user So I can-not say the input data in Correct format. Or if dash is not found then it will also give the error. I make new code please let me know this is Ok or not...? ************************************************************** public static string getNewItemName(string actualItemName, string saparaterCharacter) { string valueToReturn = ""; try { int ipos = actualItemName.ToString().IndexOf(saparaterCharacter.ToString()); if (ipos > -1) //That means saparator is not found... { if (saparaterCharacter.ToString().Length == 0) valueToReturn = actualItemName.ToString(); else { string strFirstPart = actualItemName.ToString().Substring(0, ipos).ToString(); string strSecondPart = ""; if (actualItemName.ToString().Length == 0) strSecondPart = actualItemName.ToString().Substring(ipos).ToString(); else strSecondPart = actualItemName.ToString().Substring(ipos + 1).ToString(); valueToReturn = strFirstPart + ":" + strSecondPart; } } else valueToReturn = actualItemName.ToString(); } catch (Exception) { //In Case of any error we are returning the same name valueToReturn = actualItemName.ToString(); } return valueToReturn; } **************************************************************

              Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

              S 1 Reply Last reply
              0
              • P Pankaj Joshi

                Thanks for your reply. But it will give error in case of string str = ""; Because this input is depend upon the end user So I can-not say the input data in Correct format. Or if dash is not found then it will also give the error. I make new code please let me know this is Ok or not...? ************************************************************** public static string getNewItemName(string actualItemName, string saparaterCharacter) { string valueToReturn = ""; try { int ipos = actualItemName.ToString().IndexOf(saparaterCharacter.ToString()); if (ipos > -1) //That means saparator is not found... { if (saparaterCharacter.ToString().Length == 0) valueToReturn = actualItemName.ToString(); else { string strFirstPart = actualItemName.ToString().Substring(0, ipos).ToString(); string strSecondPart = ""; if (actualItemName.ToString().Length == 0) strSecondPart = actualItemName.ToString().Substring(ipos).ToString(); else strSecondPart = actualItemName.ToString().Substring(ipos + 1).ToString(); valueToReturn = strFirstPart + ":" + strSecondPart; } } else valueToReturn = actualItemName.ToString(); } catch (Exception) { //In Case of any error we are returning the same name valueToReturn = actualItemName.ToString(); } return valueToReturn; } **************************************************************

                Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                S Offline
                S Offline
                Sun Rays
                wrote on last edited by
                #11

                Pankaj - Joshi wrote:

                public static string getNewItemName(string actualItemName, string saparaterCharacter) { string valueToReturn = ""; try { int ipos = actualItemName.ToString().IndexOf(saparaterCharacter.ToString()); if (ipos > -1) //That means saparator is not found... { if (saparaterCharacter.ToString().Length == 0) valueToReturn = actualItemName.ToString(); else { string strFirstPart = actualItemName.ToString().Substring(0, ipos).ToString(); string strSecondPart = ""; if (actualItemName.ToString().Length == 0) strSecondPart = actualItemName.ToString().Substring(ipos).ToString(); else strSecondPart = actualItemName.ToString().Substring(ipos + 1).ToString(); valueToReturn = strFirstPart + ":" + strSecondPart; } } else valueToReturn = actualItemName.ToString(); } catch (Exception) { //In Case of any error we are returning the same name valueToReturn = actualItemName.ToString(); } return valueToReturn; }

                Hi, here before checking anything you have to check that if(str.Length > 0) { if(str.Contains("-")) { // here write everything.. will not give any error. } }

                Thanks, Sun Rays To get something you must have to try once. My Articles

                1 Reply Last reply
                0
                • P Pankaj Joshi

                  I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

                  Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #12

                  string str = "ABC-X-Y-Z";
                  int index = str.IndexOf("-");
                  if (index>=0) str = str.Substring(0,index)+ "." + str.Substring(index+1);

                  :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  this months tips: - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use PRE tags to preserve formatting when showing multi-line code snippets


                  1 Reply Last reply
                  0
                  • P Pankaj Joshi

                    I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

                    Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #13

                    Yet another way:

                    if ( args.Length > 2 )
                    {
                    int index = args [ 0 ].IndexOf ( args [ 1 ] ) ;

                    if ( index >= 0 )
                    {
                        args \[ 0 \] = (new System.Text.StringBuilder ( args \[ 0 \] )).Replace ( args \[ 1 \] , args \[ 2 \] , index , args \[ 1 \].Length ).ToString() ;
                    }
                    
                    System.Console.WriteLine ( args \[ 0 \] ) ;
                    

                    }

                    1 Reply Last reply
                    0
                    • P Pankaj Joshi

                      I want to replace Dash with a period only once in a string like ABC-XYZ =ABC.XYZ ABCXYZ =ABCXYZ (No Replace) ABC-X-Y-Z =ABC.X-Y-Z A-BC-XYZ =A.BC-XYZ ABCXYZ- =ABCXYZ. I found where the first dash is match using of IndexOF property of string. And RegEx.Replace replace all the found characters I want to replace only first occurrence. But how I can replace only once...?

                      Regards Pankaj Joshi If you want to shape your dreams into reality, please wake-up...

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #14

                      How about this? Thanks for the opportunity.

                      public static string
                      Replace
                      (
                      string Subject
                      ,
                      string Old
                      ,
                      string New
                      ,
                      ushort MaxReplacements
                      )
                      {
                      System.Text.StringBuilder result = new System.Text.StringBuilder ( Subject.Length ) ;
                      string[] parts = Subject.Split
                      (
                      new string[] { Old }
                      ,
                      (int) MaxReplacements+1
                      ,
                      System.StringSplitOptions.None
                      ) ;

                      result.Append ( parts \[ 0 \] ) ;
                      
                      for ( int runner = 1 ; runner < parts.Length ; runner++ )
                      {
                          result.Append ( New ) ;
                          result.Append ( parts \[ runner \] ) ;
                      }
                      
                      return ( result.ToString() ) ;
                      

                      }

                      -- modified at 9:38 Wednesday 28th November, 2007 Dagnabit! I keep forgetting about string.Join!

                      public static string
                      Replace
                      (
                      string Subject
                      ,
                      string Old
                      ,
                      string New
                      ,
                      ushort MaxReplacements
                      )
                      {
                      return ( string.Join
                      (
                      New
                      ,
                      Subject.Split
                      (
                      new string[] { Old }
                      ,
                      (int) MaxReplacements+1
                      ,
                      System.StringSplitOptions.None
                      )
                      ) ) ;
                      }

                      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