c# - How can I compare and update the first occurrence in an array overlap? -


say have following 2 arrays...

string[] = ["word1", "word2", "word3"]; string[] b = ["word0", "word1", "word2", "word3", "word4", "word5", "word6", "word1", "word2", "word3"]; 

if want compare b , remove first occurrence in b looks this...

string[] b = ["word0", " ", " ", " ", "word4", "word5", "word6", "word1", "word2", "word3"]; 

how go this?

a straightforward way use array.indexof find first occurrence of each word a in b:

foreach (var word in a) {     var index = array.indexof(b, word);     if (index >= 0) {         b[index] = " "; // or whatever other value     } } 

note might not work expected if replacement value present inside a -- if possible should specify want happen.

update: looks want find , replace subsequence a as whole inside b, , not individual elements. different problem. 1 (naive) implementation be:

var start = enumerable.range(0, b.length - a.length + 1)                       .where(i => b.skip(i).take(a.length).sequenceequal(a))                       .defaultifempty(-1)                       .first();  if (start != -1) {     (var = 0; < a.length; ++i)     {         b[start + i] = " ";     } } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -