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
Solved

help needed with string manipulation

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

861 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