0

I have this function and I need it to format the strings in the list to be with http:// in the beginning:

        private List<string> offline(string targetDirectory)
        {

            List<string> directories = new List<string>();

            try
            {
                string[] dirs = Directory.GetDirectories(targetDirectory, "*.*", SearchOption.TopDirectoryOnly);
                for (int i = 0; i < dirs.Length; i++)
                {

                    directories.Add(dirs[i]);
                }
            }
            catch
            {

            }        
            return directories;    
        }

The function return a List of strings of directories in the hard disk. Like c:\ and c:\windows

I want that the List in the end will be instead of c:\\ and c:\\windows in index[0] and index[1] to be formatted to: http://c:\ and http://c:\windows and http://c:\temp so each string the List will be with http:// in the beginning.

How can I do it?

2 Answers 2

3

so each string the List will be with http:// in the beginning.

List<string> newList =  directories.Select(r=> "http://" + r).ToList();

Or

var list2 = directories.Select(r => string.Concat("http://", r)).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Very elegant use of linq. However if the OP cannot figure out a simple "http://" + my_old_text concatenation, it may be too much to learn at once :-(
0

Why not append while adding to list

directories.Add("http://" + dirs[i]);

Or

return directories.Select(rs=> "http://" + rs).ToList()

Or

directories.ForEach(rs=>rs= "http://" + rs);
return directories;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.