Solved

Find and replace strings

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

830 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