none
substituting part of a string with another string RRS feed

  • Question

  • Hi,

    I know there are countless string functions available in vb.net and wanted to know how to substitute a part of a string with another string, for example:

    dum="abcdefghijklmnopqrstuvwxyz"

    Now if I want to substitute the string "mnopqr" with "123456789" and have as my final value:

    dum="abcdefghijkl123456789stuvwxyz"

    I am guessing there may be a way of doing this without much coding but thought I would ask the forum for their thoughts.

    Thx in advance,

    Les

    Wednesday, March 20, 2013 6:23 AM

Answers

  • Hi Les2011,

    This code may help you :

    Dim dum As String = "abcdefghijklmnopqrstuvwxyz"
    dum = dum.Replace("mnopqr", "123456789")

    ----------------------------------

    I hope this post helps you.

    • Proposed as answer by BGQQ Wednesday, March 20, 2013 9:02 PM
    • Marked as answer by Les2011 Thursday, March 21, 2013 6:15 AM
    Wednesday, March 20, 2013 6:44 AM

All replies

  • Hi Les2011,

    This code may help you :

    Dim dum As String = "abcdefghijklmnopqrstuvwxyz"
    dum = dum.Replace("mnopqr", "123456789")

    ----------------------------------

    I hope this post helps you.

    • Proposed as answer by BGQQ Wednesday, March 20, 2013 9:02 PM
    • Marked as answer by Les2011 Thursday, March 21, 2013 6:15 AM
    Wednesday, March 20, 2013 6:44 AM
  • Do you want to replace "mnopqr" because it is at string positions 12 though 17, or do you want to replace it because it has that specific value ("mnopqr")?
    Wednesday, March 20, 2013 7:23 AM
  • Hi Acamar,

    Basically if I find a particular string withni a string that I am looking for then I want to toally replace it with the new string which can be anyting.  I just set up the example for simolicity.  It seems Mohamed has a good solution and I have not checked out the link that Cor gave me.  I will look at this tonight.  Thx all for your help.

    Les

    Wednesday, March 20, 2013 12:46 PM
  • Hi Cor,

    Will check this link out tonight when I get home.  How are things going with you?

    Thx,

    Les

    Wednesday, March 20, 2013 12:49 PM
  • Replace is the correct solution if the substring to be replaced is based on the value and not the position.
    Wednesday, March 20, 2013 9:47 PM
  • Thx Mohamed,

    Just what the doctor ordered.

    Les

    Thursday, March 21, 2013 6:16 AM