HomeGoogle DocsSpreadsheetCompare Two Strings Irrespective of the Word Positions in Google Sheets

Compare Two Strings Irrespective of the Word Positions in Google Sheets

When comparing two strings in Google Sheets, using =A1=B1 only works if the strings match exactly—including the word order. But what if the words are the same, just arranged differently?

For example, you might want to match "blue red green" with "red green blue" and return TRUE.

In this tutorial, you’ll learn how to compare two strings to check if they contain the same words, regardless of their order. You’ll also learn how to optionally ignore filler words like "the", "and", or "vs" if needed.

Let’s explore two useful methods—one that considers all words, and another that skips filler words and punctuation during comparison.

Example 1: Compare Two Strings Regardless of Word Order (All Words Considered)

This method checks if two strings contain the exact same words—no more, no less—regardless of their positions.

Sample Data

ABMatch?
cat dog mousemouse cat dogTRUE
blue green redred green blueTRUE
pen pencilpencil pen boxFALSE

We’re comparing each string in column A with the corresponding string in column B.

Formula

=JOIN(" ", SORT(TOCOL(SPLIT(A1, " ")))) = JOIN(" ", SORT(TOCOL(SPLIT(B1, " "))))

Place this in cell C1 and drag it down.

Formula Breakdown

  • SPLIT(A1, " "): Splits the string into individual words.
  • TOCOL(...): Converts the output to a single column for proper sorting.
  • SORT(...): Sorts the list of words alphabetically.
  • JOIN(" ", ...): Rejoins the sorted words into a single normalized string.
  • The same steps are applied to cell B1, and the = operator checks for equality between the two processed strings.

Example 2: Compare Two Strings While Ignoring Filler Words

This method is useful when you want to compare strings by meaning—ignoring common filler words and punctuation.

Sample Data

ABMatch?
The quick brown foxquick fox brownTRUE
A pencil and a penpen pencilTRUE
Box of chocolateschocolates boxTRUE
Brazil Vs GermanyGermany Vs BrazilTRUE

You may need to tune the filler word list based on your use case.

Formula

=JOIN(" ", SORT(TOCOL(SPLIT(
  TRIM(REGEXREPLACE(
    REGEXREPLACE(LOWER(A1), "\b(a|an|and|are|as|at|be|but|by|for|from|has|have|if|in|into|is|it|its|of|on|or|that|the|to|was|were|with|vs|vs\.|v/s)\b[.,!?;:]*", ""),
    "[^\w\s]", "")
  ), " ")))) 
= 
JOIN(" ", SORT(TOCOL(SPLIT(
  TRIM(REGEXREPLACE(
    REGEXREPLACE(LOWER(B1), "\b(a|an|and|are|as|at|be|but|by|for|from|has|have|if|in|into|is|it|its|of|on|or|that|the|to|was|were|with|vs|vs\.|v/s)\b[.,!?;:]*", ""),
    "[^\w\s]", "")
  ), " "))))

Place this in cell C1 and drag it down.

Formula Breakdown

  • LOWER(A1): Converts text to lowercase for uniform comparison.
  • REGEXREPLACE(..., "\b(...)\b[.,!?;:]*", ""): Removes filler words and attached punctuation.
  • REGEXREPLACE(..., "[^\w\s]", ""): Removes any remaining special characters.
  • TRIM(...): Cleans up leading/trailing spaces.
  • SPLIT(...), TOCOL(...), SORT(...), JOIN(...): Same as the first example.
  • The processed strings from A1 and B1 are finally compared using =.

Tip: You can customize the filler word list in \b(...)\b to suit your context.

Summary

Use CaseIgnores Filler Words?Matches Order?Allows Extra Words?
Compare word sets exactly❌ No❌ No❌ No
Compare by meaning (ignoring fillers)✅ Yes❌ No❌ No
Prashanth K V
Prashanth K V
Your Trusted Google Sheets and Excel Expert Prashanth K V is a Diamond Product Expert in Google Sheets, officially recognized by Google for his contributions to the Docs Editors Help Community and featured in the Google Product Experts Directory. Explore his blog to learn advanced formulas, automation tips, and problem-solving techniques to elevate your spreadsheet skills.

Top Discussions

More like this

How to Build a Dynamic Nonogram Clue Generator in Google Sheets

Over the past few months, I've built a couple of games in Google Sheets,...

Carpool Cost Splitter & Rotation Tracker in Google Sheets (Free Template)

Managing a carpool can be difficult, especially when you need to track whose vehicle...

How to Build a Road Trip Fuel Cost Splitter Formula in Google Sheets

Need a fair formula to split fuel costs among travelers on a long road...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.