How to check each line if it contains a word to remove that line from string?

Hi,

I have an input string which is put into one cell of excel as shown below. It is a string variable. What I am trying to do is to loop through each line that begins with a number then x beside it and check if that line contains a word for example “Tea Pack” then that line is to be removed altogether. I know there’s a command to use if string.contains(“Tea Pack”) inside the if activity. So the issue is to be able to remove that line from the orignal string so in the new string it will be removed. Any suggestion which may be the suitable approach to go about it?

Input string (in one string variable):

21x 3. Soft drink/ Bottled Water
21x 2. Nespresso Coffee Station
21x 4. Afternoon Tea Pack ( min 4 ppl ) - Chefs selection of sweet and
savoury muffins or slices
10x 1. Chips/ Nuts bag
of different flavours
21x 3. Morning Tea Pack ( min 4 ppl )
1x 8a. Standard White wine
1x 9a. Standard Red wine

Output string: (noting that the lines with the “Tea Pack” have been removed)

21x 3. Soft drink/ Bottled Water
21x 2. Nespresso Coffee Station
10x 1. Chips/ Nuts bag
of different flavours
1x 8a. Standard White wine
1x 9a. Standard Red wine

Hi,

Can you try the following?

img20210727-1

dt.AsEnumerable.Where(Function(r) not r(0).ToString.Contains("Tea Pack")).CopyToDataTable()

Sample20210727-1.zip (10.9 KB)

Regards,

Hi. Thanks for the suggestion. All the string is inside one cell of excel and not separated into more than one cell so therefore wouldn’t recognise which is a new line inside the same cell where each line starts at “number x …”. The string is assigned into a string variable type not datatable.

Hi,

How about the following?

yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"(\d+x\s\d+[\s\S]+?(?=(\d+x\s\d+|$)))",Function(m) if(m.Value.Contains("Tea Pack"),"",m.Value))

Sample20210727-2.zip (2.3 KB)

Regards,

Hi

Try below expression on string
Replace StrVal variable with respective string column in looping variable and "Tea" with the keyword that you want to search in line

String.Join(Environment.NewLine,StrVal.Split(Environment.NewLine.ToCharArray).Where(Function(x) Not(x.Contains("Tea"))).ToArray)

Let me know if this works

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.