Reputation: 77
I have a long string, and this string must break at the 15th character or earlier.
In the example string
This must break right here
the 15th character is the letter 'k', so check if it is one of { ' ' , '.', ',' }
. If it is, break the line in this position, otherwise check the preceding character until it can break. In the example it can break at character 10.
My code is only working if I can break on only one character in the equals clause:
char break = ' ';
If I use an array of possible characters (char[] canBreak = { ' ' , '.', ',' };
), it doesn't work.
Here is my code that doesn't work, I think the problem is in the Equals:
char[] canBreak = { ' ' , '.', ',' };
char break = ' ';
while (!fullMsg[breakValue].Equals(canBreak))
{
breakValue = breakValue - 1;
}
If I use only one char
instead of char[]
it works:
char[] canBreak = { ' ' , '.', ',' };
char break = ' ';
while (!fullMsg[breakValue].Equals(break))
{
breakValue = breakValue - 1;
}
What am I doing wrong?
Upvotes: 4
Views: 139
Reputation: 3545
You should use canBreak.Contains(fullMsg[breakValue])
to check if your char is one of ' ' , '.', ','
.
char[] canBreak = { ' ' , '.', ',' };
while (!canBreak.Contains(fullMsg[breakValue]))
{
breakValue = breakValue - 1;
}
This uses Enumerable.Contains<TSource>(IEnumerable<TSource>, TSource)
from System.Linq
.
Upvotes: 4