Search Unity

Need Help with Strings please

Discussion in 'Getting Started' started by CyberInteractiveLLC, Jun 19, 2019.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Suppose you have the below string

    Code (CSharp):
    1. "Release ID: Name-PTR With Version 2.0"
    How can i get what ever is after "ID:" and what ever is Before "With Version 2.0" . The Release ID: and With Version 2.0 Part is always constant. but the PTR~~~ part is not.
     
    Last edited: Jun 19, 2019
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    What is this whole string's purpose? Maybe someone can help.

    It's just continuing the same kind of assembly of the string, part by part. Do you understand what the
    List[I].Name + 
    means? You would keep doing the same sort of thing but with whatever variables has the 5555 or 1.00 information.
     
  3. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Hey i'm sorry, i made a much simpler example in original post!
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    So you have the string, and you want to extract parts of it out?

    My tool of choice is a regular expression. It's kind of a pattern-matching language designed to break apart strings into pieces which fit whatever rules you want.

    Code (CSharp):
    1. using System.Text.RegularExpressions;
    2.  
    3.     string data = "Release ID: Name-PTR With Version 2.0";
    4.  
    5.     Regex re = new Regex(@"ID: (.*) With Version");
    6.     Match m = re.Match(data);
    7.     if (m.Success)
    8.     {
    9.         string id = m.Groups[1].Value;
    10.         Debug.Log("Got identifier " + id + " out of the data.");
    11.     }
    12.  
    The above code should log

    Got identifier Name-PTR out of the data.
     
    CyberInteractiveLLC likes this.
  5. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    Yes, I want to create new string of only the part after ID: and before Ver, I will try your code, thanks!

    1 more question.. if i wanted to take out whatever comes before -PTR, from the new string, how would i do it?
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    You can extract more than one thing from the original data string all at once. Let's assume the dash is the way you keep those two parts separate.

    Code (CSharp):
    1. using System.Text.RegularExpressions;
    2.  
    3.     string data = "Release ID: Name-PTR With Version 2.0";
    4.  
    5.     Regex re = new Regex(@"ID: (.*)-(.*) With Version");
    6.     Match m = re.Match(data);
    7.     if (m.Success)
    8.     {
    9.         string namepart = m.Groups[1].Value;
    10.         string suffixpart = m.Groups[2].Value;
    11.         Debug.Log("Got name " + namepart + " and suffix " suffixpart + " out of the data.");
    12.     }
    13.  
    The above code should log

    Got name Name and suffix PTR out of the data.
     
    CyberInteractiveLLC likes this.
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    CyberInteractiveLLC likes this.