Solved

help needed with string manipulation

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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