472,800 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,800 software developers and data experts.

How do I parse name/value pairs from a config string?

If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?
Jul 21 '05 #1
4 12609
Bill,
I normally use String.Split to separate each pair, then use String.Split
again to separate the pairs.

Optionally putting the name/value pairs into a HashTable or
NameValueCollection.

Instead of the second String.Split I've used String.IndexOf &
String.SubString to split the pairs, as there should be only a single split
needed...

Hope this helps
Jay

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #2
Yep. You may use String.Split() to get individual ';' separated items and
another String.Split() (or String.StartsWith() ) on the first set to get the
password.

HTH.

"Bill" <nf*@nospam.com> wrote in message
news:Ot*************@TK2MSFTNGP12.phx.gbl...
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #3
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking
the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

al*****@yahoo.com

"Bill" wrote:
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to
retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #4
Thanks... Reqular Expressions strikes me as the way to go.

"alaspin" <al*****@discussions.microsoft.com> wrote in message
news:48**********************************@microsof t.com...
If you wanted to get really excited you could use Regular Expressions:

In VB.NET (I doubt the C# is a million miles away) :

'don't forget to import the namspace
Imports System.Text.RegularExpressions

'later...
'we're assuming a SQL Server connection string here and SQL Server
'Authentication
Dim regExp as new Regex("server=\w*;")
Dim m Match

m = regExp.Match(strDBConnectionString, "pwd=\w*;")

If m.Success Then
'you've found it...
End If

Notice the "\w*" bit is searching for a "word" in the phrase searched.

This would work with the other bits of the connection string too by tweaking the expression BUT I have found the "word" gets confused if your server is
being identified by an IP address instead of it's name...

Probably all those "."'s :)

al*****@yahoo.com

"Bill" wrote:
If, for example, I retrieve a connectionstring from a config file using
something like:

Value = ConfigurationSettings.AppSettings["ConnectionString"];

This will return a string that is semi-colon delimited. If I want, say, to retrieve the password from this string will I need to explicity parse it?

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
5
by: Robert Oschler | last post by:
I am converting a Perl script over to "C" for a potential open source project. I need some open source "C" code that will give me the same functionality of a Perl Style associative array: ...
21
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225;...
1
by: Bill | last post by:
I have strings returned from a config file that contain zero or more name/value pairs using a pattern like "name = value;" I want to extract the value of a given name (passwords, etc.). I want...
4
by: Bill | last post by:
If, for example, I retrieve a connectionstring from a config file using something like: Value = ConfigurationSettings.AppSettings; This will return a string that is semi-colon delimited. If I...
5
by: Navid Azimi | last post by:
What's the best way to parse a currency string to a decimal given the possibility of multiple currencies? That is, my numeric string can be ($12.00) or -£12.00; in either case I want -12.00 to be...
1
by: ozzii | last post by:
Hi, Does anybody know how to parse the name value pairs in a querystring contained in a variable with asp? for those of you who might be confused, what i am trying to do is basically I have a...
5
by: simononestop | last post by:
Hi im totally new to perl this is my first go at using it (I normally use asp). I have set up a form with a cgi script from demon hosting. I have edited the script and the form works it sends me an...
7
by: Cirene | last post by:
I have a long string (MyString) with several token/value combinations. Here's an example... NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE PHONE: 333-222-5151 x542 ...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.