how to extract substring from a string?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 33
Reputation: ansari.wajid is an unknown quantity at this point 
Solved Threads: 0
ansari.wajid ansari.wajid is offline Offline
Light Poster

how to extract substring from a string?

 
0
  #1
Aug 8th, 2008
I have a string stored in a variable, say
myString= <RssFeeds<link>http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml</link>
<title>CodeGuru.com</title> <description>something</description></RssFeeds><RssFeeds><link>http://lifehacker.com/index.xml</link>
<title>Lifehacker</title>
<description>something</description>
</RssFeeds>
I want to extract the text between <link>and </link>
and also the text between <title>and </title>
in two different arrays.
How to do this, please help me. if anyone have code or any helpfull link please reply back.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 155
Reputation: vuyiswamb is an unknown quantity at this point 
Solved Threads: 5
vuyiswamb's Avatar
vuyiswamb vuyiswamb is offline Offline
Junior Poster

Re: how to extract substring from a string?

 
0
  #2
Aug 8th, 2008
hi

Now Remember that i can only show you the door , you are the only person who have to walk throught it. i have created a substring and displayed them on the Messagebox, you have to assign that to the Array. here is the example code

  1. String myString = @"<RssFeeds<link>http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml</link>
  2. <title>CodeGuru.com</title> <description>something</description></RssFeeds><RssFeeds><link>http://lifehacker.com/index.xml</link>
  3. <title>Lifehacker</title>
  4. <description>something</description>
  5. </RssFeeds> ";
  6. MessageBox.Show(myString.Substring(15, 64)); /*It gave me <a rel="nofollow" class="t" href="http://www.codeguru.com/icom_includes/feeds/codeguru/rss-all.xml" target="_blank">http://www.codeguru.com/icom_include...ru/rss-all.xml</a> */
  7. MessageBox.Show(myString.Substring(99,12)); //CodeGuru.com

Hope this Helps
Last edited by Tekmaven; Aug 8th, 2008 at 4:44 pm. Reason: Code tags
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 39
Reputation: nvmobius is an unknown quantity at this point 
Solved Threads: 4
nvmobius nvmobius is offline Offline
Light Poster

Re: how to extract substring from a string?

 
0
  #3
Aug 8th, 2008
Use a System.Text.RegularExpressions.Regex object.

  1. Regex linkRegex = new Regex(@"<link>\s*(?<link>[^<]+)\s*</link>", System.Text.RegularExpressions.RegexOptions.Compiled);
  2. Regex titleRegex = new Regex(@"<title>\s*(?<title>[^<]+)\s*</title>", System.Text.RegularExpressions.RegexOptions.Compiled);
  3.  
  4. if ( linkRegex.IsMatch(myString) ) {
  5. Match match = linkRegex.Match(myString);
  6. string theLink = match.Groups["link"].Value;
  7. }
  8. if ( titleRegex.IsMatch(myString) ) {
  9. Match match = titleRegex.Match(myString);
  10. string title = match.Groups["link"].Value;
  11. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC