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 password: ". How do I do this? Any help would be appreciated. If this is still not clear, then please post a comment.

6 Answers

6

I think

userString.Contains("stringToSearchFor")

would return either true or false.

Thank you so much. I'm an idiot for not thinking of that.

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();

     }

}

}

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 Mono/.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.

you can do this

if(yourString in stringToSearchFor)

here is an easy example for doing this

var passwordEnterString : String;
var correctPassword : String;

function Update()
{
         if(correctPassword in passwordEnterString)
                passwordEnterString = "correct";

}

wouldnt this be too insecure? I mean you asking if your password string for example "password123" is inside the givin' input, or am i miss interpretate it? if the input string = "abcdefghijklmnopqrstuvwxyz1234567890!§$%&/()=?" it always should say > passwordEnterString = "correct"; because all letters are givin

Aren’t strings immutable? I’m starving for a similar method for destroying clones on a ticker.