Solved

Find and replace strings

Posted on 2011-02-22
4
242 Views
Last Modified: 2012-05-11
Hi Experts,

I have a problem; I have a string called mystring as:
mystring = "ABC1254,ACB859,BCA9875"

I would like firstly to only get the first three characters of each item of mystring (each item is divided by a comma)
Then the result will be like this:
mystring = "ABC,ACB,BCA"

Finally, can I replace each item with its corresponding number? Such as:
ABC= 1
ACB =2
BCA =3

So, the final result is:
mystring = "1,2,3"

Note: I am using vb.net 2010
Thanks in advance
Regards
0
Comment
Question by:Sat80
[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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
klakkas earned 500 total points
ID: 34950964
Using Split and Substring, you can achieve what you want.

Here is a code sample:

Dim mystring As String = "ABC1254,ACB859,BCA9875"
        Dim Res As String = ""
        For Each part As String In mystring.Split(Convert.ToChar(","))
            Select Case part.Substring(0, 3)
                Case "ABC"
                    Res += "1,"
                Case "ACB"
                    Res += "2,"
                Case "BCA"
                    Res += "3,"
            End Select
        Next
        mystring = Res.Substring(0, Res.Length - 1)

Open in new window

0
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 34950969
First split the string into it's items:

  Dim items as String() = mystring.Split(","C)

Then you can get the first characters of each item:

  For i As Integer = 0 To items.Length - 1
    items(i) = items(i).Substring(0, 3)
  Next

Create a dictionary for the translations:

  Dim replace As New Dictionary(Of String, String)
  replace.Add("ABC", "1")
  replace.Add("ACB", "2")
  replace.Add("BCA", "3")

Check each string for a translation:

  For i As Integer = 0 To items.Length - 1
    If replace.ContainsKey(items(i)) Then
      items(i) = replace(items(i))
    End If
  Next

Put the items back into a string again:

  mystring = String.Join(",", items)
0
 

Author Closing Comment

by:Sat80
ID: 34950986
Thanks!
0
 

Author Comment

by:Sat80
ID: 34950995
Sry GreenGhost, I did not see your comment before accepting the first reply. Thank you for the help.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

751 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