Replacing a substring at a specific position


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: Replacing a substring at a specific position

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    7

    Replacing a substring at a specific position

    Hi all, need some help.

    I want to replace the second character in a string, regardless of what character that is.

    If I try the replace the second character with the (substring 1, 1) all instances of that character gets replaced.
    I just want to replace the one sitting at position 2 in the string.

    Ex Good -> Gxod, not Gxxd

    Thanks in advance

    Patrik

  2. #2
    Join Date
    Dec 2004
    Posts
    163
    paarfal,

    One option is to use the classic BASIC MID statement:

    Dim myString As String = "abcde"

    Mid(myString, 2, 1) = "x"

    MsgBox(myString)

    (Of course, now the .Net purists will respond that you should not use the MID statement.)

    Kerry Moorman

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    In VB, you can use the Mid statement:

    Mid(theString, 2, 1) = "x"

    There's no equivalent in C#. You could add a reference to the Microsoft.VisualBasic namespace and call the Mid statement from C#, or you could convert the string to an array of characters, update the desired character, then create a new string from the char array.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  4. #4
    Join Date
    Jul 2004
    Posts
    81
    In VB, you could have (add semi-colon for C#)
    myString = myString.Remove(1, 1).Insert(1, "x")

    Of course, not quite as direct as the Mid statement.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  5. #5
    Join Date
    Nov 2005
    Location
    Jaipur
    Posts
    14
    in C# you can use in this way
    TextBox2.Text =TextBox1.Text.Replace(TextBox1.Text.Substring(1,1),"p");

    here p is the newly replaced char.
    thanks
    raj

  6. #6
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Raj: I'm pretty sure that will replace all occurrences of the second character with the letter 'p'. So, "good" would become "gppd".
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  7. #7
    Join Date
    Apr 2005
    Posts
    7
    Hi all,

    Thaks for your suggestions. I'll try work it out based on them.

    Regards,

    Patrik

  8. #8
    Join Date
    May 2006
    Posts
    1
    I think the best in C# would be:
    TextBox2.Text =TextBox1.Text.Remove(1,1).Insert(1,"p");
    cheers

  9. #9
    Join Date
    May 2006
    Posts
    3

    why not use LEFT and MID ?

    Why couldn't you just use ...
    newstring = LEFT(oldstring, 1) & newcharacter & MID(oldstring, 3)

  10. #10
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    That would work, but it's less efficient than Mid(theString, 2, 1) = "x" due to its use of concatenation.
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  11. #11
    Join Date
    May 2006
    Posts
    3

    Talking

    Absolutely, but it is simpler to understand for new programmers. And the performance hit is only noticable if the number of iterations on it are extremely large.

  12. #12
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    OK, how about this:

    ' Replace second character of theString with 'x'
    Mid(theString, 2, 1) = "x"

    That's efficient and easy for new programmers to understand (and it doesn't teach them bad habits ;-).
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  13. #13
    Join Date
    May 2006
    Posts
    3

    Cool

    Works for me! Personally I have always used MID() = X in the manner you describe. Although I wouldn't call the concatenation a "bad habit" ... just not the most efficient one.

  14. #14
    Join Date
    Oct 2007
    Posts
    1

    C# replace specific character

    A bit of filthy wat to do it, but it works.

    string editString = "Good";

    StringBuilder sb = new StringBuilder(editString);
    sb.Remove(1, 1);
    sb.Insert(1, "x");

    editString = sb.ToString();

Similar Threads

  1. Replies: 1
    Last Post: 10-25-2005, 08:59 AM
  2. Replies: 0
    Last Post: 06-14-2005, 11:39 AM
  3. Mouse Position
    By Ranger in forum VB Classic
    Replies: 2
    Last Post: 12-07-2000, 01:29 PM
  4. Mouse Position
    By Ranger in forum VB Classic
    Replies: 0
    Last Post: 12-07-2000, 01:23 PM
  5. Replies: 2
    Last Post: 04-13-2000, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center