Solved

Find and replace strings

Posted on 2011-02-22
4
239 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
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
klakkas earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0
 

Author Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

763 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

8 Experts available now in Live!

Get 1:1 Help Now