Power Automate中替换字符串第3次出现的指定字符方法咨询
Since you're working in Power Automate (given the "Apply to each" mention), here's a reliable way to target and replace the 3rd occurrence of | with | Product: in each string of your array:
Step-by-Step Expression
Inside your "Apply to each" loop, use this expression for the current item (item()):
join( take(split(item(), ' | '), 3), ' | ' ) & ' | Product: ' & join( skip(split(item(), ' | '), 3), ' | ' )
How This Works
Let’s break down the logic using your first example string:"Periods: 01.01.2023 - 01.02.2023 | Code: 111 | Code2: 1111 | product-name"
split(item(), ' | ')splits the string into an array:["Periods: 01.01.2023 - 01.02.2023", "Code: 111", "Code2: 1111", "product-name"]take(..., 3)grabs the first 3 elements, thenjoin(..., ' | ')reconstructs the first part of the string:"Periods: 01.01.2023 - 01.02.2023 | Code: 111 | Code2: 1111"- We append
' | Product: 'to add the new field label. skip(..., 3)takes everything from the 4th element onward (your product name), andjoinputs it back together (this handles edge cases where product names might contain|too, even if your example doesn't have this scenario).
Result for Your Example
After applying the expression, your first string becomes:"Periods: 01.01.2023 - 01.02.2023 | Code: 111 | Code2: 1111 | Product: product-name"
This method avoids the pitfalls of trying to count occurrences directly (which can fail if earlier parts of the string have unexpected | characters) and works consistently for all items in your array.
内容的提问来源于stack exchange,提问作者Artemy Panin

