Solved

vb.net replace

Posted on 2010-11-15
5
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

617 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