- Home /
Check if string contains some text
I want to make a javascript check if a string contains one of two pieces of text. I figured it out, but it doesn't work. Here is my code:
var XZ : String;
//X or Z?
//0=X, Y=Z
function Update(){
XZ.Contains("X"||"Z");
if (true){
Debug.LogError("XZ must be either X or Z");
}
}
What happens is it always returns the error no matter what. What's wrong with this?
Answer by Eric5h5 · Aug 06, 2013 at 05:43 PM
Code doesn't work like that. Contains requires you to use a string; "X"||"Z" is not a string. Also, if (true) is always true, so it's pointless to do that. I assume you're after something like
if (XZ.Contains("X") || XZ.Contains("Z")) {
Thanks for that, I was really confused about what I was doing.
I have a question for this. what if there are two gamobjects. These names are "Object" and "Object Front". How can i check only "Front" word in if - else statement? does not work Contains. is there another way?
If you have a question then post a question. Comments on answers isn't the place for it.
Your answer
Follow this Question
Related Questions
List is empty when used in other method 1 Answer
if a string has certain letter(s) or number(s) 2 Answers
string.Contains() to work with string with Chinese characters? 1 Answer
I am Trying to find what position a Space is in my Array and get them to a array of floats 2 Answers
How do you check if a string has already been removed from a generic list? 1 Answer