Solved

vb.net replace

Posted on 2010-11-15
5
492 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
ID: 34140420
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
ID: 34141809
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
ID: 34142433
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
ID: 34142454
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
ID: 34142563
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

20 Experts available now in Live!

Get 1:1 Help Now