Solved

vb.net replace

Posted on 2010-11-15
5
491 Views
Last Modified: 2012-06-27
hello there,
how can I replace some data from a string.. for example I have multiple "name1" in a string but I would like to replace from the first "name1" to "name2"
how can I do that?
0
Comment
Question by:XK8ER
5 Comments
 
LVL 7

Expert Comment

by:EYoung
Comment Utility
Does the first "name1" occur in the same place?

If not, you could use an IF statement to test if the string exists.  If it does exist, then do the replace using the substr function, then EXIT the IF statement.
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
Comment Utility
See String.Replace method...
    Dim s As String = "some text, name1, some more text, name1"
    s = s.Replace("name1", "name2")

Open in new window


Wayne
0
 
LVL 7

Expert Comment

by:EYoung
Comment Utility
Are you trying to replace just the first occurance or all occurances of "name1"?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
Comment Utility
Replacing only the first occurrence could be done with:
Dim s As String = "some text, name1, some more text, name1"



        Dim find As String = "name1"

        Dim replaceWith As String = "name2"

        Dim index As Integer = s.IndexOf(find)

        If index <> -1 Then

            s = s.Remove(index, find.Length)

            s = s.Insert(index, replaceWith)

        End If

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
Comment Utility
Try this:
Dim oldStr As String = "some string name1 new string name1 test"

Dim newStr As String = Replace(oldStr, "name1", "name2", , 1)

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now