Hello,
Very easy task, I know :)
Searching the internet for a simple .NET method to do that for me brought to me this page inside Bytes.com: Count all occurrences of a character in a string
I think they have come with working solutions, but why writing a method or a loop to do this simple task.
It came to my mind that removing the character from the string will decrease the string Length with the character occurrence. Good but .NET does not have that Remove(char) Method, alright we can use Replace(char, char) or Replace(string, string). Lets do that.
Lets say we have a string called search and a char called c.
Subscribing the string after the characters are removed (or replaced with nothing) from the original string will result the occurrence. Very simple, one line.
I hope it will work for someone!
Regards,
Bassem
Very easy task, I know :)
Searching the internet for a simple .NET method to do that for me brought to me this page inside Bytes.com: Count all occurrences of a character in a string
I think they have come with working solutions, but why writing a method or a loop to do this simple task.
It came to my mind that removing the character from the string will decrease the string Length with the character occurrence. Good but .NET does not have that Remove(char) Method, alright we can use Replace(char, char) or Replace(string, string). Lets do that.
Lets say we have a string called search and a char called c.
Code:
int occurrence = search.Length -
search.Replace(c.ToString(), string.Empty).Length;
I hope it will work for someone!
Regards,
Bassem
Comment