Solved

help needed with string manipulation

Posted on 2006-06-22
5
193 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
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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

19 Experts available now in Live!

Get 1:1 Help Now