Using LIKE in a replace function

Hi,

I am a beginner and currently enjoying a lot to learn UI Path

I am reading a DataTable from an excel sheet. In a “Each Row” loop im reading the values within the column “EType”. In case a value contains the string “Meal”, value need to be replaced with “Meals”.

I have created an assignment activity with the below logic. No errors are split out - However, after looking at the output file the values were not replaced.

any ideas where the problem is? The replace function in an if else statement works perfectly if i not using the LIKE operator.

If(Convert.ToString(row(“EType”)).Equals(“LIKE ‘%Meal%’”), Convert.ToString(row(“EType”)).Replace(“LIKE ‘%Meal%’”, “Meals”), Convert.ToString(row(“EType”)))

My second question:

Can i add a new if else statement on a different column using the OR function? So i dont need to create multiple assignments for every case where values needs to be replaced. Below an example which doesn’t work unfortunately

If(Convert.ToString(row(“EType”)).Equals(“Breakfast”), Convert.ToString(row(“EType”)).Replace(“Breakfast”, “Meals”), Convert.ToString(row(“EType”)))
OR
If(Convert.ToString(row(“EType”)).Equals(“Dinner”), Convert.ToString(row(“EType”)).Replace(“Dinner”, “Meals”), Convert.ToString(row(“EType”)))
OR
If(Convert.ToString(row(“XYZ”)).Equals(“Hello”), Convert.ToString(row(“XYZ”)).Replace(“Hello”, “Whats up”), Convert.ToString(row(“XYZ”)))

Thanks in advance!

:slight_smile: you need to use the Convert.ToString(row(“EType”)).Contains(" Meal") for single check if you want to check multiple then
Note : Contains(" Meal") this will give you true or false value on the given value,
you can update the this by writing below code

if(Convert.ToString(row(“EType”)).Contains(" Meal"))
{
Convert.ToString(row(“EType”)).Replace(“Hello”, “Whats up”)*
}

1 Like

Thanks Mukesh,

But in the replacement function the old string could be anything which contains “Meal”. Any ways i can use a generic value instead “Hello”? I want to replace any value if the condition is met with “Meal”.

Something like below

If(Convert.ToString(row(“EType”)).Contains(" Meal "))
{
row(“Expense Type”) = “Meal”
}

If(Convert.ToString(row(“EType”)).Contains(" Meal "))
Above code code give you that value id present in test

Convert.ToString(row(“EType”)).Replace(“old string value”, “new string value”)*

if you looking for an exact match then you need to use below code
if(row(“Expense Type”) == “Meal”)
{
ow(“Expense Type”).Replace(“old string value”, “new string value”)*
}

1 Like