how to check starting/ending word in a long space delimited string

29 views Asked by At

I have a string array i.e.

string[] input_Text = new string[] { "i am wanting to take admission in the univeristy of the islamabad", "we are enjoying all the talent here at the city of atlanta", "what are you doing there" };

and a stopWords array i.e.

string[] stopWords = new string[] { " are ", " am ", " all ", " at ", " here ", " i ", " in ", " of ", " take ", " the ", " there ", " to ", " what ", " we ", " you " }; 

I have to replace stopWords in input_Text with " " (whiteSpace) but issue is that i am having " i " in stopWords array and text contains 'i' at start mean there is no whitespace at start of 'i'. So issue is the starting and ending word strings in text doesn't match with the stopWords, so unable to remove those words. The loop I'm using is....

for (int i = 0; i < input_Text.Count(); i++) 
 { 
   for (int j = 0; j < stopWords.Count(); j++) 
   {
      input_Text[i] = input_Text[i].Replace(stopWords[j], " ");
   } 
  }

Any suggestions will be highly appreciated.

1

There are 1 answers

0
ycsun On BEST ANSWER

Given your data, you can add a space character to the beginning and end of input_Text before Replace, and remove them afterwards:

string s = " " + input_Text[i] + " ";
s = s.Replace(stopWords[j], " ");
input_Text[i] = s.Substring(1, s.Length - 2);

Not very efficient, but should work fine.