Solved

help needed with string manipulation

Posted on 2006-06-22
5
189 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
  • 2
  • 2
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
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
Comment Utility
Good way Kemo Sabe.

Bob
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

6 Experts available now in Live!

Get 1:1 Help Now