Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-03
5
Medium Priority
?
279 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 64

Accepted Solution

by:
Fernando Soto earned 1200 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 800 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 64

Expert Comment

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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

773 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