I am writing an application that will check text entered into a rich text box and see if it contains kanji (chinese characters) outside of a specific list.
I am currently using a string array for the 1000 kanji I want to allow and I’m just doing a for loop over each element in the user input and checking to see if the kanji is in the list or not. If it’s not, I return a list at the end of all the ‘not allowed’ kanji.
My questions is, right now I am creating the string array inside the check method that is run when the user hits the ‘check’ button but I’m afraid that means I am creating the 1000 kanji list every time the operation is done. Where and how do I load the 1000 kanji string array so that it’s always loaded into memory? It there any way to just initialize the pre-made list (I have no need to add or remove elements – I’m using it almost like a dictionary).
You’re better of storing the characters in a List collection and use List.Contains to see if a character is on the list:
Create and fill in ‘myCharacters’ list only once. Call myCharacters.Contains() method whenever you need to check for a character. Contains() method is optimized so it will be way more performant than your own implementation. Below is a full blown sample: