Solved

Find and replace strings

Posted on 2011-02-22
4
243 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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