Remove a list of characters from string in C# -
i need method removes list of characters string. need support characters i'm not sure i'm managing surrogate characters properly.
is there better way?
public static string remove(string source, char[] oldchar) { if (string.isnullorempty(source) || oldchar == null || oldchar.length == 0) return source; (int = source.indexofany(oldchar, 0); != -1; = source.indexofany(oldchar, i)) source = source.remove(i, char.issurrogatepair(source, i) ? 2 : 1); return source; } thank you
frank
the following not deal surrogate pairs not clear question want do. if fulfils requirement of removing list of characters string
public static string remove(string source, char[] oldchar) { return string.join("", source.tochararray().where(a => !oldchar.contains(a)).toarray()); } example:
var s = "hello world"; var c = new[] { 'l', 'o' }; remove(s, c); //returns: wrd
Comments
Post a Comment