Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:10:53+00:00 2026-06-09T14:10:53+00:00

I have a question for how to compare two strings. here is the code.

  • 0

I have a question for how to compare two strings.

here is the code.

        string stringA = "This is a test item";
        string stringB = "item test a is This";

Obviously, stringB contains every words from stringA, but in a different order.

My desired result should be TRUE.

My question is, what should I do? I have tried to use the .Contains() method, but the result is FALSE.

Thanks everyone.

UPDATES

Thanks everyone for the kindly replies.

Here is my clarification

I am actually building a database search function by using LINQ and EF.

Assume that, an item has its name as “This is a test item”.

If user input “test a is this”, I would like the function smart enough to catch the item mentioned above.

Any suggestion?

ANOTHER UPDATE

Thanks again for all your help.

I do like Peter Ritchie’s, codesparkle’s, Dave’s and EdFred’s suggestion.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:10:55+00:00Added an answer on June 9, 2026 at 2:10 pm

    Building on Peter Richie’s excellent suggestion, using Array.Sort() instead of List<T>.Sort(), without the duplication but packed into a neat extension method:

    public static bool ContainsSameWordsAs(this string first, string second)
    {
        return first.GetSortedWords().SequenceEqual(second.GetSortedWords());
        // if upper and lower case words should be seen as identical, use:
        // StringComparer.OrdinalIgnoreCase as a second argument to SequenceEqual
    }
    
    private static IEnumerable<string> GetSortedWords(this string source)
    {
        var result = source.Split().ToArray();
        Array.Sort(result);
        return result;
    }
    

    Usage

    string stringA = "This is a test item";
    string stringB = "item test a is This";
    string stringC = "Not the Same is This";
    bool result = stringA.ContainsSameWordsAs(stringB);    // true
    bool different = stringA.ContainsSameWordsAs(stringC); // false
    

    Edit: It’s hard to understand why you accepted an answer that does not comply with the requirements stated in your question. If you really want the strings "This is a test item" and "test a is this" to match, you’d need to use something a bit more involved, such as:

    public static bool ContainsSameWordsAs(this string first, string second)
    {
        var ignoreCase = StringComparer.OrdinalIgnoreCase;
        return first.Split().Any(word => second.Split().Contains(word, ignoreCase));
    }
    

    You may want to come up with a better algorithm though, as this one is extremely loose — two identical words will be enough to count as a match. But this one will match your requirements as stated in the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a follow-up to this question , I need to compare two strings in
I have two identical strings, one in an array and one in a String
I'm trying to use this function to compare two strings, case insensitively. int strcasecmp(const
I have question. I have some app on facebook and getting this error Fatal
I have question i.e how can i select a previous div CODE:- if($i >
I have a webform with two custom validators: One to validate that a string
I have a variable s which contains a one letter string s = 'a'
I read this question and another question . In one of these two questions,
This is a question about an authentication scheme. Say I have a shared secret
I have a function that should be passed a string with two numbers (as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.