0

can i get string from string contains? when i type "asdsahello1" how to get string on list only "hello1" in contains string.

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?
        }
    }
}
}
2
  • You can use for( int i=0 ; i<array.Length ) loop here. Given if(true) break;, i will be index you're looking for Commented Jul 31, 2020 at 8:49
  • It should probably be inputText.Contains(x) rather than inputText.Contains.x. Commented Jul 31, 2020 at 8:51

2 Answers 2

1

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. It gets called each time the Inputfield is updated.

Sign up to request clarification or add additional context in comments.

Comments

0
getString=x;

Varible x contains value that you are testing.

2 Comments

x = hello1 and hello2
Ye, your string can conteins some your test strings e.g. "asdsahello1asdasdsahello2". And you can get all matches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.