?
Solved

Return a string of List content

Posted on 2012-08-10
3
Medium Priority
?
552 Views
Last Modified: 2012-08-15
Hi,

I need to get the content of the list and assign it to a string.
I am getting an error at   return finalString;

Any idea?
Thanks!


public List<string> GetLinksFromString(string strLinks)
        {
            string finalString = "";

            List<string> list = new List<string>();
            Regex urlRx = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*", RegexOptions.IgnoreCase);
            MatchCollection matches = urlRx.Matches(strLinks);
            foreach (Match match in matches)
            {
                list.Add(match.Value);
            }

            foreach (string link in list)
            {
               finalString = (link);
            }
            return finalString;
            }

Open in new window

0
Comment
Question by:Rad1
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38282933
Your function is returning a LIST of string:

    public List<string> GetLinksFromString(string strLinks)

If you want it to return a string, then change it to:

    public string GetLinksFromString(string strLinks)

Note that your code at lines #13 thru #17 will iterate thru all the strings in the list you just built and then simply return the LAST one.

If you want to return all the strings as ONE, then how do you want to DELIMIT them?
0
 

Author Comment

by:Rad1
ID: 38298666
I want to return all the text from the textbox and the links separated  with spaces.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38298768
This will return the links separated by spaces:
        public string GetLinksFromString(string strLinks)
        {
            List<string> list = new List<string>();

            Regex urlRx = new Regex(@"((https?|ftp|file)\://|www.)[A-Za-z0-9\.\-]+(/[A-Za-z0-9\?\&\=;\+!'\(\)\*\-\._~%]*)*", RegexOptions.IgnoreCase);
            MatchCollection matches = urlRx.Matches(strLinks);

            foreach (Match match in matches)
            {
                list.Add(match.Value);
            }

            return String.Join(" ", list.ToArray());
        }

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Integration Management Part 2

807 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