Looking for guidance on how I can get the code below to produce a list of indexes of every white-space and hyphen character in longString
.
Whenever I run the statements below, it just keeps adding 5 to my integer list. Given the string I'm using as an example below, I'm trying to have it collect 5, 11, 14, 24, and 27.
static void Main(string[] args)
{
string longString = "hello world my user-name is stevieray8450";
int longStringLength = longString.Length;
char whiteSpace = ' ';
char hyphen = '-';
// list to store all indexes of white space
List<int> specialIndexes = new List<int>();
foreach (char c in longString)
{
if (c.Equals(whiteSpace) || c.Equals(hyphen))
{
specialIndexes.Add(longString.IndexOf(c));
}
}
I'm racking my brain as to why the char c
in my foreach
loop is evaluating to 5 every time for white-space in my specialIndexes.Add(longString.IndexOf(c));
statement.
Any thoughts, opinions, comments welcome :) Thanks!