0

I found a few solutions similar to my question but they are not helpful. I have an array list containing few string values { "Apple", "Banana", "AppleBanana" }

I want to be able to extract Apple from each string. Below is an extraction word "Apple". But how do I extract Apple from AppleBanana word?

string[] listOfString = { "Apple", "Banana", "Apple", "AppleBanana" };
var query = from string i in listOfString
            group i by (i == "Apple") into appleString
            select appleString;

foreach (var item in query)
{
    if (item.Key == true)
    {
        string[] Applyby = item.ToArray();

        foreach (var item2 in item)
        {
            Console.WriteLine(item2);
        }
    }
}
7
  • 3
    what is your expected output, after extraction? Commented Sep 8, 2015 at 7:16
  • Instead of i == "Apple" use i.Contains("Apple"), in general, you can figure out this sort of stuff by reading the documentation or even by playing with intellisense in visual studio. Commented Sep 8, 2015 at 7:17
  • I am looking to display Apple Apple. Commented Sep 8, 2015 at 7:18
  • 2
    What means "extract"? You have already the word you search, so there's nothing to extract. Also, if your expected output is Apple Apple you don't want to find the Apple in AppleBanana. Can you explain the rules better? Commented Sep 8, 2015 at 7:22
  • 1
    So should { "Apple", "Banana", "Apple", "AppleBanana" } result in { "Apple", "Apple" } or { "Apple", "Apple", "Apple" }? Commented Sep 8, 2015 at 7:27

5 Answers 5

1

how about

string[] listOfString = { "Apple", "Banana", "Apple", "AppleBanana" };
var AppleItems = listOfString.Where(x => x.Equals("Apple"));
foreach (string item in AppleItems)
{
    Console.WriteLine(item);
}

returns Apple Apple

Sign up to request clarification or add additional context in comments.

1 Comment

@TicketForLessUK did you try my approach?
0

You can try using C# Regex Class

string text = "AppleBanana";
Regex pattern = new Regex( "Apple" );
Match match = pattern.Match( text );

string value = groups[ 1 ].Value;

Comments

0

I guess you also want to find substrings. Then you can't use == but either String.Contains or String.IndexOf. The latter has the advantage that you can make it case-insensitive easily.

String.Contains:

var allWithApple = listOfString.Where(word => word.Contains("Apple"));

String.IndexOf:

var allWithApple = listOfString
    .Where(word => word.IndexOf("apple", StringComparison.InvariantCultureIgnoreCase) >= 0);

You can execute the LINQ queries with a foreach or create a new String[] with ToArray.

foreach(string wordWithApple in allWithApple)
    Console.WriteLine( wordWithApple );

2 Comments

Not helping @Tim, I am still getting results with AppleBanana.
@TicketForLessUK: remember your own words: "But how do I extract Apple from AppleBanana word". If you don't want it you dont need to search substrings. Then you can use == or Equals.
0
string[] listOfWords = { "Apple", "Banana", "Apple", "AppleBanana" }; //this is case-sensitive
listOfWords.Where(x => x.Contains("Apple")).Select(a => Console.WriteLine(a));

This will print Apple Apple AppleBanana (in new lines). If you want just "Apple" for all the words, you can simply hard code that.

string[] listOfWords = { "Apple", "Banana", "Apple", "AppleBanana" };
listOfWords.Where(x => x.Contains("Apple")).Select(a => Console.WriteLine("Apple"));

Comments

0
string[] listOfString = { "Apple", "Banana", "Apple", "AppleBanana" };
            string searchStr="Apple";
            int count=0;
            foreach (string subStr in listOfString)
            { 
            if (subStr.Contains(searchStr))
                count +=1;
            }
     Console.WriteLine("The occurence of " & searchStr & "is :" &  count);
 // here count give the number of apples in the array

Or else you can extract the words that contains Apple as like the following:

            string[] listOfString = { "Apple", "Banana", "Apple", "AppleBanana" };
            string searchStr="Apple";   
       Console.WriteLine("The following strings contains the word : " & searchStr );
            foreach (string subStr in listOfString)
            {
                if (subStr.Contains(searchStr))
                     Console.WriteLine(subStr);
            }

1 Comment

Both your solutions gives me blank screen.

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.