Skip to content

Instantly share code, notes, and snippets.

@JayKickliter
Created August 10, 2013 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayKickliter/6200905 to your computer and use it in GitHub Desktop.
Save JayKickliter/6200905 to your computer and use it in GitHub Desktop.
C# function to removed words at it specified indices in a string. No protection for out of bound indices.
static string RemoveWordsAtIndices(string theString, int[] indices)
{
Array.Sort(indices);
int originalWordCount;
int newWordCount;
int numberOfWordsToRemove;
string[] originalWordArray;
string[] newWordArray;
originalWordArray = theString.Trim().Split(' ');
numberOfWordsToRemove = indices.Length;
originalWordCount = originalWordArray.Length;
newWordCount = originalWordCount - numberOfWordsToRemove;
newWordArray = new string[newWordCount];
int i = 0;
int j = 0;
int k = 0;
while ( i < originalWordCount )
{
if ( i != indices[j] )
{
newWordArray[k] = originalWordArray[i];
k++;
} else if (j != numberOfWordsToRemove - 1) {
j++;
}
i++;
}
return String.Join(" ", newWordArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment