Find Substring of a String in Another String

Hi, I have two strings, 0017-Part and INV-0017 | P/O 9711,
I need to check if the second string contains any substring of the first string.

1 Like

I’ll assume str1 = β€œ0017-Part” and str2 = β€œINV-0017 | P/O 9711”

You can do this within an if activity, or an assign activity (result is a boolean): str2.contains(str1)

1 Like

Hi Dave,
so the thing is str2.contains(str1) will return false, but I need it to return true because 0017 is in both str1 and str2

Ok sorry I misunderstood. Do you have a predefined portion that you want to look at within str1? If not, you are going to find matches because str2 contains β€œ0”,β€œ1”,β€œ7”,β€œ-”,β€œP”.

It seems like you want to match the β€œ0017” portion though - is there a way that you can define how it will always appear? Is it always digits in the first part of the string? If so, you could use regex to pull out those digits only. Is it always going to be the first part preceding the β€œ-”? If so, you could split the string by the β€œ-” symbol, and take the first element in the array.

Either way, you want to determine how to pull out the substring from str1 that you actually want to search, then use that substring you pulled out to use the str2.contains(str1substring) formula