Solved

Help with creating string values based on another string values using VB.NET

Posted on 2013-11-03
5
272 Views
Last Modified: 2013-11-04
Hi,

I have string S with the following fixed values  S = BEL,CAN,FRA,DEU,USA

If I have string S1 = 0,2

How do I create a new string S2 = BEL,FRA?

The values in S  should relate to 0,1,2,4  where

BEL relates t0 0
CAN relates to 1
FRA relates to 3 etc...
0
Comment
Question by:vcharles
  • 2
  • 2
5 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 39620412
Hi vcharles;

This code snippet will give you what you want.

' Your initial string
Dim S As String = "BEL,CAN,FRA,DEU,USA"

' Convert the string S into a collection that you can index into
Dim S_Collection As List(Of String) = S.Split(New String() {",", " "}, StringSplitOptions.RemoveEmptyEntries).ToList()
' Convert the string S1 into a collection of integers so that it can be used to index into above collection
Dim S1 As String = "0,2"
Dim S1_Index_Collection As New List(Of Integer)
For Each i As String In S1
    If i = "," OrElse i = " " Then Continue For
    Dim idx As Integer = Integer.Parse(i)
    S1_Index_Collection.Add(idx)
Next

Dim S2 As String = ""
' Now you can get the values from S_Collection
For Each idx As Integer In S1_Index_Collection
    If S2.Length = 0 Then
        S2 = S_Collection(idx)
    Else
        S2 += "," & S_Collection(idx)
    End If
Next

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 39620423
Another one:
        Dim S As String = "BEL,CAN,FRA,DEU,USA"
        Dim S1 As String = "0,2"

        Dim countries() As String = S.Split(",")
        Dim codes() As Integer = Array.ConvertAll(S1.Split(","), Function(x) Int32.Parse(x))

        Dim output As New List(Of String)
        For Each code As Integer In codes
            output.Add(countries(code))
        Next

        Dim finalCountries As String = String.Join(",", output.ToArray)
        MessageBox.Show(finalCountries)

Open in new window

0
 

Author Comment

by:vcharles
ID: 39620649
Thank You, will try them and get back to you.
0
 

Author Closing Comment

by:vcharles
ID: 39622245
Thank You.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39622326
Not a problem, glad to help.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 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