Make a string Generic

I want to compare two strings.

str1 = “Hello {user_name}! Welcome to {store_name} How may I help you?”
str2 = “Hello Awais Ali! Welcome to KHAADI How may I help you?”

Now I want to compare and get results TRUE. {user_name} and {store_name} are always be different.

Can someone help me?
Thanks in advance…

Hi,

Do you want to compare str1 and str2. You could give this a try → String.compare() or String.contains() (if you know what parameters you are looking for.)

@awaisji2001 You can also try with Str1.equals(Str2)

Best Regards,
Vrushali

I try this String.compare() or String.contains() but it returns false but i want it returns TRUE

Hi,

Str1.equals(Str2) also returns false but I wana TRUE .

@awaisji2001 Could you please tell me what input you are providing?

I try this

"Hello {user_name}! Welcome to {store_name} How may I help you?".Equals("Hello Awais Ali! Welcome To KHAADI How may I help you?")

@awaisji2001 are user_name and store_name variables?

No, I want to skip {user_name} and {store_name} from this string "Hello {user_name}! Welcome to {store_name} How may I help you?" and then it will be "Hello ! Welcome to How may I help you?"

Now I want to compare "Hello ! Welcome to How may I help you?" with "Hello Awais Ali! Welcome To KHAADI How may I help you?" and get results TRUE.

Actually, I haven’t {user_name} and {store_name}.

Hi @awaisji2001 ,

Please try this
str1 = “Hello Awais Ali! Welcome To KHAADI How may I help you?”
str2 = “Hello {user_name}! Welcome to {store_name} How may I help you?”
in assign str2 = str2.Replace(" {user_name}“,”“).Replace(”{store_name}“,”")
str1.contains(str2)

I try this but still returns false

If you want to compare the strings while ignoring the user name and store name, you need to use regular expression.

str1 = "Hello Awais Ali! Welcome To KHAADI How may I help you?"
strPattern = "Hello [\w ]+! Welcome to [\w ]+ How may I help you\?"

result = System.Text.RegularExpressions.Regex.IsMatch(str1, strPattern)

image

1 Like

Hi ptrobot

I try this regular expression System.Text.RegularExpressions.Regex.IsMatch(str1, strPattern) but it returns me false

Hi! Sorry, I tested the pattern with a text with lower case t while your text has a capital T. Change the T to lower case or add the options IgnoreCase.

result = System.Text.RegularExpressions.Regex.IsMatch(str1, strPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)

image

image

1 Like

Yeah @ptrobot,

It works fine I also did not attention to that “t” or “T”…
Thanks a lot for resolving my problem…

1 Like

Hi @ptrobot

Give me one more favor that how to match these two strings…

str1 = "Commerce BotE-commerce Website30 March at 19:34Tues 19:34You sent 30 March at 19:34Get StartedCommerce Bot sent 30 March at 19:34Hello Usman Ashraf! Welcome to KHAADI How may I help you? Commerce Bot sent 30 March at 19:34Would you like to check out the following items:Promotions 🎁Top Products 🏆Catalogue 📜You sent 30 March at 19:34restartCommerce Bot sent 30 March at 19:34Your conversation has been restarted.You sent 30 March at 19:34helloCommerce Bot sent 30 March at 19:34Hello Usman Ashraf! Welcome to KHAADI How may I help youCommerce Bot sent 30 March at 19:34Would you like to check out the following items:Promotions 🎁Top Products 🏆Catalogue 📜"

str2 = "Hello [\w ]+! Welcome to [\w ]+ How may I help you\?"

While in str1 Hello Usman Ashraf! Welcome to KHAADI How may I help you? is present.

If you just want to find out if the Hello text is somewhere in str1, change the expression to:

result = System.Text.RegularExpressions.Regex.Matches(str1, strPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase).Count > 0

image

what should the regular expression for this ??

With the Hello text, I mean the whole string “Hello …! Welcome to … How may I help you?”. Are you saying you want to match “Commerce BotE-commerce …” and all the other text too? Because that regular expression would not like pretty.