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

x
?
Solved

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

Posted on 2013-11-03
5
Medium Priority
?
277 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
[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
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

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