Unity里面实现简单关键词查找
******************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Com.Rainier.WangJing
{
public class KeySearch : MonoBehaviour
{
public static KeySearch Instance;
private void Awake()
{
Instance = this;
}
void Start ()
{
}
/// <summary>
/// 关键词检索结果
/// </summary>
public struct KeySearchResult
{
public bool isContain;//是否匹配到关键词
public int keyCount;//匹配到的关键字个数
public List<int> keyIndex;//匹配到的关键词索引
}
/// <summary>
/// 关键词匹配
/// </summary>
public KeySearchResult IsKeySearch(string inputStr, string[] targetKey)
{
KeySearchResult result = new KeySearchResult();
result.keyIndex = new List<int>();
for (int i = 0; i < targetKey.Length; i++)
{
string key = targetKey[i];
if (inputStr.Contains(key))
{
result.isContain = true;
result.keyCount++;
result.keyIndex.Add(i);
}
}
return result;
}
}
}
有问题请指出,谢谢!