I think I’m hitting the same problem. I’ve done something like you’ve suggested but kind of boiled it down in the hopes to just get the basics of what I/we are trying to accomplish. So made a string consisting of a sentence that contains several words that begin with “a”… and that’s as far as I’ve got.
I figured from there I could use IndexOf to search through the string for the letter “a”, but then realised that could come back with the index of that letter at any point in a word. So changed it to search for " a" (an “a” with a space before it), but that’s when I hit my next block. I’ve no idea what to write to get the code to take that information and somehow pick out the the word that is starting at that point in the string, and only that word. I did think of doing so kind of double index check to find a space following where the " a" was found, but after that I don’t know how to get it to print out the information between those two points.
I’ve tried Googling around and found things about checking to see what a string starts with, or if it starts with a particular character/word. Or the same but at the end of the string.
The closest I found to make a string for the poem/sentence, another for the what we’re looking for (the start - it would be “a” but again we need to be sure its the start of a word, so this is " a"), and what we’re looking for after that (a space to indicate the end of the word). Then make an if statement with a condition looking at the sentence containing the start, when that’s true AND when it’s also true that it finds in the sentence what we’ve declared is the end.
Then to declare start and end points and create a substring using them. Most of this is actually doable with what we’ve previously been taught, EXCEPT the if condition uses this: String.Contains. I don’t remember seeing this - I may be wrong, and there may be another way around it but this is all I’ve found so far.
So what I have is:
strSource = the poem;
strStart = " a";
strEnd = " ";
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
int Start, End;
Start = (strSource.IndexOf(strStart, 0) - 1) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
Console.WriteLine(strSource.Substring(Start, End - Start));
I’ve then tried putting this inside a ‘for’ loop, with the thought that I’d assign a boolean value to, say, ‘i’. Set it to ‘true’, then in the ‘if’ statement put “i = strSource…”. Also pop “continue;” at the end of the ‘if’ section. My thinking here is that it would loop through until it got to the end of the poem/sentence and then I’d have it stop. But sadly on trying this I just kept getting errors about my ‘for’ condition.