Check is a string contains text

I want to convert input from a GUI.TextField to numbers (which I have done) but how do I check if it contains text?

Current code

void windowObjective_C(int windowID) { objective_CTextFieldString = GUI - Pastebin.com (don’t know how to format code on Unity forums)

I need a if check to see if the objective_CTextFieldString contains text like 53x = false, 53 = true, x = false.

There is .Contains()

but probably you should do the logic some other way… What do you actually want to accomplish?

I wanna increase a number in a variable that affects how the game reacts. Well short an experience system. People earn points and then use those points for xp/other stuff. Any recommendations for some other logic, I’m new to Unity.

yeah. Int.parse. or int.tryparse.

Google those two things…they allow you to parse a string and see if the value passed in was an int.

TryParse will work. However, I’ve also found it cumbersome and weird, preferring try-catch with int.Parse:

bool containsNonNumberText = false;
int numberParsed = -1;
try {
numberParsed = int.Parse(yourText);
}
catch {
containsNonNumberText = true;
}

That seems a lot more cumbersome than just

int num;
if (int.TryParse(myText, out num))
{
    DoNumberThings(num);
}

Yeah, idk what you’re doing with that bool in there…tryparse won’t return anything if the text isn’t a number…so…not sure why you’re doing what you’re doing.

TryParse returns a bool; if it returns true, it found a valid number, if it returns false, it didn’t. If it found a number, it will change the value of the second parameter you feed it, to that number.

I guess cumbersome wasn’t really the word, just weird.

Looks odd at first but is pretty standard. Conceptually works the same as Dictionary.TryGetValue and Physics.Raycast

1 Like