How to replace different words from list found in string with specific word "x" :
string str = "how to replace different words from list found in string with specific word";
var valueList = new List<string> { "replace", "string", "specific", "found", "how", "word"};
if (valueList.Any(str.Contains))
{
//...
}
if with if-statement I can do it one by one str.Replace("replace", "x");
etc., but not sure how to properly get same result from listed values:
x to x different words from list x in x with xx
With Linq flavour:
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
string str = "how to replace different words from list found in string with specific word";
var valueList = new List<string> { "replace", "string", "specific", "found", "how", "word"};
var result = valueList.Aggregate(str, (current, c) => current.Replace(c, "x"));
Console.WriteLine(result);
}
}
EDIT:
.NET Fiddle: https://dotnetfiddle.net/BVJphf
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.