简体   繁体   English

如何在字符串中统一获取包含 c# 的字符串?

[英]how to get string in stringcontains c# in unity?

can i get string from string contains?我可以从包含的字符串中获取字符串吗? when i type "asdsahello1" how to get string on list only "hello1" in contains string.当我键入“asdsahello1”时,如何在列表中获取字符串,只有“hello1”包含字符串。

public class FindContainsText : MonoBehaviour
{
private string[] test = { "hello1", "hello2" };
public Inputfield inputText;

void Update()
{
  foreach (string x in test)
    {
       if(inputText.Contains(x))
        {
        string getString;
        getString=//inputText.Contains(x) how?
        }
    }
}
}

Try this code snippet:试试这个代码片段:

public class FindContainsText : MonoBehaviour
{
    private string[] test = { "hello1", "hello2" };
    public InputField inputText;

    public void Update()
    {
        int matchIndex = -1;
        for (int i = 0; i < test.Length; i++)
        {
            if (inputText.text.Contains(test[i]))
            {
                matchIndex = i;
                break;
            }
        }

        if (matchIndex != -1)
        {
            Debug.Log($"Input field contains {test[matchIndex]} (element number {matchIndex})");
        }
        else
        {
            Debug.Log("No match!");
        }
    }
}

Also consider using onValueChanged event instead of Update.还可以考虑使用onValueChanged事件而不是 Update。 It gets called each time the Inputfield is updated.每次更新输入字段时都会调用它。

getString=x;

Varible x contains value that you are testing.变量 x 包含您正在测试的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM