- Home /
string.Contains() to work with string with Chinese characters?
Hi, I'm trying to make a text box that will search for strings containing the characters that the user has inputted in the text box so far from a string array. I have tried using the string.Contains() and it works great with strings containing English strings.
But for my use case, the string array will contain strings with both English and Chinese, and it would not work for the Chinese part. Below is the code that I used. I originally thought the problem was the ToLower() part, but even if I removed them, the string.Contains() doesn't seem to work with the Chinese characters. Does it have to do with character encoding or something? I'm really lost, would greatly appreciate if someone could point me to the right direction? Thanks so much!
public UIInput inputField;
private string[] names = new string[5];
void Start () {
names[0] = "Mr. Peter Andrews";
names[1] = "Ms. Amy Walter";
names[2] = "陳大文";
names[3] = "李小文";
names [4] = "陳大東";
}
void Update () {
inputOriginal = inputField.value.ToString ();
inputLower = inputField.value.ToString ().ToLower ();
if (inputLower.Length != countStr) {
if (inputLower.Length != 0){
foreach (string searchResult in names){
string searchLower = searchResult.ToLower();
if (searchLower.Contains (inputLower)){
Debug.Log(searchResult);
}
}
}
countStr = inputLower.Length;
}
}
Answer by Graham-Dunnett · Jun 07, 2014 at 10:11 AM
Works for me in a new project. Note that I used:
public string inputField;
and
if (inputLower.Contains (searchLower)){
So I could use the inspector to edit the string, and I wanted to check if that string contains any of the words in the array.
thanks for your reply. Yes the code works as long as the contents are in English. I tried to substitute some of the names to Chinese and then the Contains() function doesn't seem to be working properly. I'm wondering if there is anything I need to do with encoding of the text...