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
Solved

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

Posted on 2013-11-03
5
273 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 63

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 63

Expert Comment

by:Fernando Soto
ID: 39622326
Not a problem, glad to 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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 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