Solved

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

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now