Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find and replace strings

Posted on 2011-02-22
4
Medium Priority
?
245 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

715 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