Created
August 10, 2013 15:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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