[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

help needed with string manipulation

Posted on 2006-06-22
5
Medium Priority
?
205 Views
Last Modified: 2012-05-05
Hi

I was wondering if you could show me how to do the following.

I have a string which is a series of variable = value pairs. Each set is separated by a semi colnn. Some of the values can themselves be a comma separated list.

Eg:
name=andrea;email=andrea@world.com;roles=admin,superuser;

I need to split this input. I would ideally like the result to be in a hashtable or soemthing where the key is the variable name. In the case of the roles variable, i would like the different roles to be stored in an arraylist. Could i store an array list as the value in a hashtable?

In this example, the hastable would contain the following:

key      value
name   andrea
email   andrea@world.com
roles    arraylist = (admin,superuser)

How would i go about doing this? I am awful with string manipulation in vb.net

Thanks very much
andrea
0
Comment
Question by:andieje
[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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 16964265
Here is one way...

Public Class Form1

    Private ht As Hashtable

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim key, value As String
        Dim vals() As String
        Dim data As String = "name=andrea;email=andrea@world.com;roles=admin,superuser;"

        ht = New Hashtable
        For Each pair As String In data.Split(";".ToCharArray)
            vals = pair.Split("=".ToCharArray)
            If vals.Length = 2 Then
                key = vals(0)
                value = vals(1)
                If Not ht.ContainsKey(key) Then
                    If value.IndexOf(",") <> -1 Then
                        ht.Add(key, New ArrayList(value.Split(",".ToCharArray)))
                    Else
                        ht.Add(key, value)
                    End If
                End If
            End If
        Next


        ' show the results...
        Dim al As ArrayList
        For Each de As DictionaryEntry In ht
            If TypeOf de.Value Is ArrayList Then
                al = CType(de.Value, ArrayList)
                Debug.WriteLine("Key: " & de.Key & ", Value(s): ")
                For Each value In al
                    Debug.WriteLine(vbTab & value)
                Next
            Else
                Debug.WriteLine("Key: " & de.Key & ", Value: " & de.Value)
            End If
        Next
    End Sub

End Class
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16964381
Good way Kemo Sabe.

Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16964413
You crack me up Bob...  =)

Read the "Lone Ranger Lore" at Wiki:
http://en.wikipedia.org/wiki/The_Lone_Ranger
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16964438
I love the one from Gary Larson, with The Lone Ranger looking up Kemo Sabe in a dictionary and finding that it means, "an Apache expression for a horse's rear end."

Bob
0
 

Author Comment

by:andieje
ID: 16964504
Thanks very much!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

649 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