- Home /
How to tell if a string contains some specified text
Hi, everyone. I was wondering if there was a way to check if there was a way to tell if the user enters some text into a text area and do something about it. That is, the user inputs "Login" at the end of a string and I want it to delete "Login" and add "Type the password \n password: ". How do I do this? Any help would be appreciated. If this is still not clear, then please post a comment.
Answer by by0log1c · Apr 10, 2011 at 08:18 PM
I think
userString.Contains("stringToSearchFor")
would return either true or false.
Thank you so much. I'm an idiot for not thinking of that.
Answer by Meltdown · Apr 10, 2011 at 07:48 PM
If you're using C#, take a look at the IndexOf() and ToUpper() string methods.
If you're using JavaScript, take a look at the IndexOf() and toUpperCase() methods.
Sorry, but my example must have mislead you. What I actually want to do is have the user input "Login" at the end of a string and for it to delete "Login" and add "Type the password \n password: ". I will edit my question accordingly.
String methods are part of $$anonymous$$ono/.net; they're not related to C#. They work in all languages. JS in Unity doesn't have "toUpperCase()", it uses .net, so all the string methods are the same as in other languages that use .net.
Answer by Kenneth Sills · Apr 11, 2011 at 12:43 AM
If you are using C# then heres an example, with comments:
//Load your stuff
using System.Collections;
using System.IO;
namespace example
{
public void justAnExample() {
//Just a streamreader to read a string from a doc
StreamReader sr = new StreamReader("C:/Whatever/example.txt")
//Heres what you want, sr.IndexOf("LALALA") finds where the string of "LALALA" is located //in the stringreader. If it is not there it's -1, so if it's greater than -1 it's there.
if (sr.IndexOf("LALALA") > -1)
{
doStuff();
}
}
}
Answer by santi5655 · Jun 07, 2013 at 11:00 PM
here is an easy example for doing this
var passwordEnterString : String;
var correctPassword : String;
function Update()
{
if(correctPassword in passwordEnterString)
passwordEnterString = "correct";
}
Your answer
Follow this Question
Related Questions
The code see the target First Person Controller only 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Resize GUI TextArea with text height 2 Answers
What is the best way to concatenate string to create an interaction logging console? 4 Answers
Setting Scroll View Width GUILayout 1 Answer