473,503 Members | 9,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to get particular string in .net

21 New Member
hi

i have following string

string1="provider=pervasive oledb; datasource=test; location=mycomputer"

now i want to fetch the string
string2=test (i.e.= whatever in my datasource=)

and string3=mycomputer (i.e.=whatever in my location=)

also the string in string1 is not fixed
this is windows application in vb.net

Can anyone help me in finding the solution...

thanks in advance....
Nov 15 '08 #1
16 1612
Frinavale
9,735 Recognized Expert Moderator Expert
Use the String.Split() method and split the string on ";". This will give you an array of Strings and you can do with them as you please.

-Frinny
Nov 16 '08 #2
balabaster
797 Recognized Expert Contributor
or you could use regular expressions:

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
  2. Dim str2 As String = Regex.Match(str1, "?i:(?<=datasource=)[^;]+(?=;)").Value
You could use the same idea for any of the settings, just replace datasource with the new item... i.e. for the location:

"?i:(?<=location=)[^;]+(?=;)"

Just to give you some clue as to what's going on, check out the regular expression cheat sheet for .NET found at: http://regexlib.com/CheatSheet.aspx
Nov 16 '08 #3
Frinavale
9,735 Recognized Expert Moderator Expert
or you could use regular expressions:
I don't know why I always forget about regular expressions.
They are very handy especially with Strings.

Thanks Balabaster :)
Nov 16 '08 #4
balabaster
797 Recognized Expert Contributor
No problem... I can't count the number of times though where I've thought immediately to use regular expressions for something and then after having spent an hour trying to perfect a regular expression, I could've done something far quicker to implement... like by splitting a string into an array...
Nov 16 '08 #5
kunal pawar
297 Contributor
I guess this is ur setting so ur application.
So datasource and other things are changed after installation.
So why ur not use app.config ?
Nov 17 '08 #6
dipalipatel
21 New Member
or you could use regular expressions:

Expand|Select|Wrap|Line Numbers
  1. Dim str1 As String = "provider=pervasive oledb; datasource=test; location=mycomputer"
  2. Dim str2 As String = Regex.Match(str1, "?i:(?<=datasource=)[^;]+(?=;)").Value
You could use the same idea for any of the settings, just replace datasource with the new item... i.e. for the location:

"?i:(?<=location=)[^;]+(?=;)"

Just to give you some clue as to what's going on, check out the regular expression cheat sheet for .NET found at: http://regexlib.com/CheatSheet.aspx
Hi,
Thanks for your Answer.
But i am not sure which dll i need to add in my code so that i can get that method Regex.match.....
Can you send me that dll??

Thanks
Dipali
Nov 19 '08 #7
kunal pawar
297 Contributor
u have to use
System.Text.RegularExpressions;
Nov 19 '08 #8
dipalipatel
21 New Member
u have to use
System.Text.RegularExpressions;

Thanks Kunal...
I tryed that and it gives me the follong error

parsing "?i;(<=Data Source=)[^;]+(?=;)" - Quantifier {x,y} following nothing.


Can you help with this?

Thanks
Nov 20 '08 #9
balabaster
797 Recognized Expert Contributor
The symbol after the ?i at the beginning should be a colon (:) not a semi-colon (;)...
Nov 20 '08 #10
dipalipatel
21 New Member
The symbol after the ?i at the beginning should be a colon (:) not a semi-colon (;)...
Hi

I did that but still i get the error. So, here is what i did:

My Original string:
mystr ="Provider=PervasiveOLEDB;Data Source=BBData;Location=MyComputer"

Part of string that i want as below:
Getstr = System.Text.RegularExpressions.Regex.Match(mystr, "?i:(<=Data Source=)[^;]+(?=;)").Value;

Error:
parsing "?i:(<=Data Source=)[^;]+(?=;)" - Quantifier {x,y} following nothing.

Can you help with this?

Thanks
Nov 20 '08 #11
balabaster
797 Recognized Expert Contributor
Oops, I missed another omission in your match string:

Expand|Select|Wrap|Line Numbers
  1. string MatchString = "(?i:(?<=Data Source=)[^;]+(?=;))";
Replace your match string with that...you didn't copy it right originally...

You missed a ? before <=Data Source and you need to wrap your string in another set of parenthesis...
Nov 20 '08 #12
dipalipatel
21 New Member
Oops, I missed another omission in your match string:

Expand|Select|Wrap|Line Numbers
  1. string MatchString = "(?i:(?<=Data Source=)[^;]+(?=;))";
Replace your match string with that...you didn't copy it right originally...

You missed a ? before <=Data Source and you need to wrap your string in another set of parenthesis...
Hey, You are great!!!

Thanks a Lot for your Solution.
Actually i was looking for a 1 line solution instead of Split and all function.
I Appreciate your help

Once again Thanks a lot...
Nov 20 '08 #13
balabaster
797 Recognized Expert Contributor
Hey, You are great!!!

Thanks a Lot for your Solution.
Actually i was looking for a 1 line solution instead of Split and all function.
I Appreciate your help

Once again Thanks a lot...
Well a one line solution would just involve putting the regex into a method something like:

Expand|Select|Wrap|Line Numbers
  1. private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
  2.  
  3. public string GetSetting(string SettingName)
  4. {
  5.     return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
  6. }
From which you could extract each setting as easily as:
Expand|Select|Wrap|Line Numbers
  1. public main()
  2. {
  3.     string strDS = GetSetting("Data Source");
  4.     string strProv = GetSetting("Provider");
  5.     string strLoc = GetSetting("Location");
  6. }
Piece of cake really...
Nov 20 '08 #14
dipalipatel
21 New Member
Well a one line solution would just involve putting the regex into a method something like:

Expand|Select|Wrap|Line Numbers
  1. private string SettingsString = "Provider=pervasive oledb; Data Source=test; Location=mycomputer";
  2.  
  3. public string GetSetting(string SettingName)
  4. {
  5.     return Regex.Match(SettingsString, String.Format("?i:(?<={0}=)[^;]+(?=;)").Value, SettingName));
  6. }
From which you could extract each setting as easily as:
Expand|Select|Wrap|Line Numbers
  1. public main()
  2. {
  3.     string strDS = GetSetting("Data Source");
  4.     string strProv = GetSetting("Provider");
  5.     string strLoc = GetSetting("Location");
  6. }
Piece of cake really...

Hi !!
Very Nice Solution
but Regex.Match has only 2 arguments in string, the 3rd one is System.Text.RegularExpressions.RegexOptions ..
And in Soutions you pass 3 Strings, So it gives an error regarding that 3rd arg.
Nov 20 '08 #15
balabaster
797 Recognized Expert Contributor
Not so much an extra setting as a ) in the wrong place:

Expand|Select|Wrap|Line Numbers
  1.         public static string GetSetting(string SettingName) 
  2.         { 
  3.             return Regex.Match(SettingsString, String.Format("(?i:(?<={0}=)[^;]+(?=;|$))", SettingName)).Value; 
  4.         } 
  5.  
  6.         static void Main(string[] args)
  7.         {
  8.             string strDS = GetSetting("Data Source");
  9.             string strProv = GetSetting("Provider");
  10.             string strLoc = GetSetting("Location");
  11.  
  12.             Console.WriteLine(strDS);
  13.             Console.WriteLine(strProv);
  14.             Console.WriteLine(strLoc);
  15.             Console.ReadKey();
  16.         }
I made one slight modification to the match string also - because it wouldn't pick up the last setting if you omitted the closing ";", I added an "|$" after the ";" so that it would check for ; or end of string.

Sorry, usually I try and test my code before posting it, but I didn't have access to Visual Studio when I posted last time...
Nov 20 '08 #16
dipalipatel
21 New Member
It's Working ...
Great..

Thanks... a lot once again
Nov 20 '08 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2852
by: Larry C | last post by:
Hello, I have an XML doc and I am trying to write a utility thath will allow me to change a particular word with my "title" section of theXML doc. I would like to look for a "word" in the title...
3
1862
by: Scott M. Lyon | last post by:
I'm trying to figure out the best way (considering there could be instances where I get a lot of data in this XML, and I want to minimize any slowdowns) to extract the value of one particular node...
2
1361
by: .NET Developer | last post by:
Hello, I'm trying to write a RegEx that will find all occurances of a particular type of HTML anchor <a> element in a big block of HTML. Here are the pattern requirements - they consist of certain...
4
44941
by: CodeRazor | last post by:
I am reading an xml file from a URL. I was originally doing this using the XmlDocument class. But this was very slow. The XmlTextReader is meant to be much quicker for forward only retrieval of...
3
1899
by: Hari | last post by:
Hi all, I need to get a part of string which follows a pattern 'addr=' For example: a)test="192.168.1.17:/home/ankur/nios_fson/mnt/tmptype nfs(rw,addr=192.168.1.17)"
1
3734
by: venkat9200 | last post by:
I need to find out how many bytes a particulat language text takes. In my application i have a display limit for the unicode string. So i need to find the no of bytes a particular language text...
3
3500
by: Cplusplusbeginner | last post by:
I need to write a program to search a particular string in a specific file.e.g if the file is abc.dat which contains names of students in a class, I need to search for a particular name say John in...
0
1496
by: ajithmanmadhan | last post by:
hi pls help me out in inserting text into a richtextbox. the situation is like this:: i want to insert a string say "hello" at a particular location in the rich textbox. The particular...
1
1494
by: dsudhakara | last post by:
In my queues.conf file my text message are stored here .I want to retrieve particular campaign modify some words in particular campaign and return to save the same queues.conf file .Plz give me...
0
1049
by: nityaprashant | last post by:
Hello.. could u plz help me? i have template column inside grid view.. in that one link button called Remove this? when i click on that link button confirmation box like "Are u sure u want...
0
7095
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7294
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7470
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3173
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.