You can use the following formula to find the longest string in a row or the longest string in each row in Google Sheets:
=BYROW(range, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))
In this formula, replace range
with the actual range you want to apply it to.
Example of Finding the Longest String in a Row
Assume you want to find the longest string in the range B2:F2
.
Insert the following formula in G2
:
=BYROW(B2:F2, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))

Example of Finding the Longest String in Each Row
If you want to find the longest string in each row in the range B2:F6
, use this formula:
=BYROW(B2:F6, LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row))))))
This will return the longest string(s) from each row separately.
Formula Explanation
Let’s break down the concept behind the formula.
Start with:
=FILTER(B2:F2, LEN(B2:F2)=MAX(LEN(B2:F2)))
Here:
- FILTER picks the values in
B2:F2
where the length matches the maximum length in that row. - If there are multiple strings tied for the longest length, it will return all of them.
To join tied values into a single text string, we wrap it with TEXTJOIN:
=TEXTJOIN(", ", TRUE, FILTER(B2:F2, LEN(B2:F2)=MAX(LEN(B2:F2))))
The TEXTJOIN
function combines the tied values into one string, separated by commas.
Now, to make this reusable, we convert it into a custom LAMBDA function:
LAMBDA(row, TEXTJOIN(", ", TRUE, FILTER(row, LEN(row)=MAX(LEN(row)))))
Here, row
acts as a placeholder for any row you apply it to.
Finally, we use BYROW to apply this formula across a row or each row:
=BYROW(array_or_range, lambda)
Replace array_or_range
with B2:F2
(for a single row) or B2:F6
(for multiple rows), and use the custom lambda
function we just created.
That’s your formula to find the longest string in a row or the longest string in each row in Google Sheets.
FAQs
How do I find the First String with the Longest Length in a Row?
If you want only the first longest string instead of all tied ones, modify the LAMBDA
function like this:
LAMBDA(row, INDEX(row, XMATCH(MAX(LEN(row)), LEN(row))))
Example:
=BYROW(B2:F6, LAMBDA(row, INDEX(row, XMATCH(MAX(LEN(row)), LEN(row)))))
Does this Work with Numbers?
Yes! This approach works with numbers as well, based on the character length of the number (not its numeric value).