Fast way to check if string is `mostly` capital

Viewed 253

I'm writing a bot in C# for a chat room and I want to detect if a message contains too many capital letters. A message contains too many capital letters if its total uppercase letter count exceeds one-third of its total message length and if its total length is greater than 13. This is to prevent smaller messages from being flagged.

Right now I am looping through each character and seeing if it is uppercase. This is fine for a reasonably lengthed message. However, if you get a spammer or troll, they will not always post reasonably lengthed messages. The maximum character limit is 2000 and I cannot change this. With my method, it starts stalling around 500+ characters. This gives the spammer enough time to paste the message and send it again, effectively flooding the chat while the bot struggles to keep up.

The code I have currently:

bool isMostlyUpper = (message.Count(c => char.IsUpper(c)) >= message.Length * 0.3f) && message.Length > 13;

I can't compare the message to string.ToUpper() because I still want to detect if the message is mostly uppercase as opposed to all uppercase.

Is there a way to do this without looping over each char? Or a way to get to the result faster? I can add checks to see if the message is > 500 but sometimes there are 500+ long messages that are okay to pass through.

Does anyone have any clever solutions? Thanks.

2 Answers
Related