Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Miscellaneous text in a string?

Discussion in 'Scripting' started by greatwhiteshark17283, Oct 20, 2015.

  1. greatwhiteshark17283

    greatwhiteshark17283

    Joined:
    Feb 18, 2013
    Posts:
    15
    I was wondering how to have an if statement that could know if a certain part of a string doesn't matter. For example, if I have this script:

    Code (JavaScript):
    1. var txt : String;
    2.  
    3. function Start () {
    4. if(txt == "I am"+anything){
    5. print("Hello");
    6. }
    7. }
    I would want to replace where it says "anything" with something that would allow the script to ignore whatever comes after "I am". If someone were to type in "I am Sam", "I am Lisa", "I am Bill", etc., it would would always print("Hello"). Is there a way to do this?
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    if (txt.Startswith("I am"))
    ...
     
    greatwhiteshark17283 likes this.
  3. greatwhiteshark17283

    greatwhiteshark17283

    Joined:
    Feb 18, 2013
    Posts:
    15
    Do you happen to know a way to save whatever "anything" would be as a variable?
     
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Take a look at the string class reference from the MSDN. It has a list of the string manipulation functions you want.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    anything = txt.Remove(0,4)
     
  6. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    525
    Code (CSharp):
    1. if (string.Contains("aSubsetOfMyText"))
    2.     // do stuff
    Cheers
     
    greatwhiteshark17283 likes this.
  7. greatwhiteshark17283

    greatwhiteshark17283

    Joined:
    Feb 18, 2013
    Posts:
    15
    Thanks to everyone for your help. It wasn't exactly what I was looking for, but I've founds ways to make it work with what you've all said. If anyone knows how to make "anything" act kind of like "%s" as used in Python, that's really what I was trying to find out. Like I said, I've found ways to work around this, so this isn't something that I desperately need.
     
  8. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Code (CSharp):
    1. string name = "John";
    2. string formated = string.Format("Hello {0}.",  name);