943,809 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 20097
  • C# RSS
Aug 8th, 2008
0

how to extract substring from a string?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ansari.wajid is offline Offline
47 posts
since Aug 2007
Aug 8th, 2008
0

Re: how to extract substring from a string?

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

C# Syntax (Toggle Plain Text)
  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" 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
Reputation Points: 31
Solved Threads: 14
Posting Whiz
vuyiswamb is offline Offline
310 posts
since Mar 2007
Aug 8th, 2008
0

Re: how to extract substring from a string?

Use a System.Text.RegularExpressions.Regex object.

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 4
Light Poster
nvmobius is offline Offline
39 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: import image to db
Next Thread in C# Forum Timeline: schedulinga task





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC