user3276223
user3276223

Reputation: 59

Finding keywords in string[] getting results to a list

I have a string[] containing ie:

abcd

Saving C:\\xx 

aacd

Saving C:\\yy

aecd

Saving C:\\zz 

and so on

Is there a way in linq that searches all the lines and returns only C:\\xx, C:\\yy and C:\\zz to a list/array.

This is what i tried so far:

string[] line = result.Split(new[] { '\r', '\n' });
string searchTerms = "Saving ";

var results = (from comp in line.ToString()
               where searchTerms.All(s => comp.Contains(s))
               select comp).ToList();

Upvotes: 0

Views: 74

Answers (5)

Sani Huttunen
Sani Huttunen

Reputation: 24395

If all you want to do is get the paths from a string you can use regular expressions for this:

var result = "abcd\r\nSaving C:\\xx\r\naacd\r\nSaving C:\\yy\r\naecd\r\nSaving C:\\zz\r\n";
var regex = new Regex(@"(?<=Saving )(.*)");
var matches = regex.Matches(result).Cast<Match>().Select(m => m).ToList();

Edit

Another more generic form of regular expression you can use to get all paths (not just the ones with Savingbefore them):

var matches = Regex.Matches(result, ".:\\\\(.*?)(?=\s.*)", RegexOptions.Multiline).Cast<Match>().Select(m => m).ToList();

This also removes any whitespace after the paths.

Edit 2

To also get paths within quotes (paths with spaces) you can use this regular expression:

var regex = new Regex("((\"\\w|\\w):\\\\)((.*\")|(.*?)(?=\\s.*))");

Upvotes: 0

Kjartan
Kjartan

Reputation: 19151

This should give you a simple IEnumerable containing each path.

NOTE: This will only return the part of each line after whatever is in searchTerms.

var result = 
      line.Where(l => l.StartsWith(searchTerms))
          .Select(c => new string(c.Skip(searchTerms.Count()).ToArray()));

Upvotes: 0

FoggyFinder
FoggyFinder

Reputation: 2220

If string ends path:

        string result = @"abcd
Saving C:\xx 
aacd
Saving C:\yy
aecd
Saving C:\zz 
and so on";

            string[] line = result.Split(new[] { '\r', '\n' });
            string searchTerms = "Saving ";
            var lst = line.Where(x => x.StartsWith(searchTerms))
                .Select(y => y.Substring(searchTerms.Length));
            foreach (var x in lst)
                Console.WriteLine(x);

http://ideone.com/OXDMO5

Upvotes: 0

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101731

You don't need All method, you can just use:

where comp.StartsWith(searchTerms)

And instead of splitting on \r and \n it's better to use:

string[] line = result.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

And you should also remove ToString from line.ToString()

var results = (from comp in line
               where comp.StartsWith(searchTerms)
               select comp).ToList();

Upvotes: 1

P.Petkov
P.Petkov

Reputation: 1579

I don't, really understand your kind of input data, but if it is like this

string[] str = new string[] { "abcd", "Saving C:\\xx", "aacd", "Saving C:\\yy", "aecd", "Saving C:\\zz"};

then you can do the following:

List<string> result = str.Where(s => s.StartsWith("Saving C:\\")).ToList();

Upvotes: 0

Related Questions