Question

How would I store the letter in a string that has been removed from a word?

DataRow row = myDataTable.Rows[rowIndex];
RandomWord = row["Words"].ToString();
Letter = RandomWord.Remove(2, 1);

This is my code so far, it gets a word from the row 'Words' and then removes 1 letter from index 2 and stores it as Letter. Now how would I store the letter that has been removed?

Was it helpful?

Solution

You'd have to capture the letter before removing it:

string Letter = RandomWord.Substring(2, 1);
RandomWord = RandomWord.Remove(2, 1);

or if you want a char:

char Letter = RandomWord[2];
RandomWord = RandomWord.Remove(2, 1);

OTHER TIPS

How about using an extension method and overload string.Remove ?

public static class Extensions
{
    public static string Remove(this string source, int start, int count, out char letter)
    {
        letter = source[start];
        return source.Remove(start, count);
    }
}

Usage:

string str = "Word";
char letter;
str = str.Remove(2, 1, out letter);  // letter = r
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top