Search Unity

string[] get specific username from String Array

Discussion in 'Getting Started' started by heipp123, Oct 16, 2015.

  1. heipp123

    heipp123

    Joined:
    Aug 1, 2015
    Posts:
    8
    i was following Dreamlo Tutorial


    in Void FormatHighscores
    Code (CSharp):
    1. void FormatHighscores(string textStream) {
    2.         string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
    3.         highscoresList = new Highscore[entries.Length];
    4.        
    5.         for (int i = 0; i <entries.Length; i ++) {
    6.             string[] entryInfo = entries[i].Split(new char[] {'|'});
    7.             string username = entryInfo[0];
    8.             int score = int.Parse(entryInfo[1]);
    9.             highscoresList[i] = new Highscore(username,score);
    10.         }
    11.     }
    12.  
    i put
    Code (CSharp):
    1.   print (highscoresList[i].username + ": " + highscoresList[i].score);
    its print all users on the leaderboard, but how do i print specific name from the array

    thanks.
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Lol

    @heipp123 What you're asking is probably a different function than the code you posted. I guess you could do it by comparing the username to a specific string, and only printing it if they match, but there's probably a better way.

    The code that you added is doing exactly what it's supposed to be doing, though, so it's working properly.
     
  4. heipp123

    heipp123

    Joined:
    Aug 1, 2015
    Posts:
    8

    Sorry i am really bad explaining. i was trying to check if my player Name is on the list and If its it would print my name and my score.

    Thanks
     
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Nah, you're good. I understood exactly what you were going for. I may not have done a good enough job explaining how to approach your goal.

    Not to offend you, but do you understand what the code that you posted does? If not, and you want to continue developing games, it's pretty important to learn. Thankfully, Unity provides a great tutorial series to get started.

    If you're not into learning and just want to do this and be done with it, modify the code you had posted to this:
    Code (CSharp):
    1. void GetAndPrintPlayerScore(string textStream, string playerName) {
    2.   string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
    3.   for (int i = 0; i < entries.Length; i ++) {
    4.     string[] entryInfo = entries[i].Split(new char[] {'|'});
    5.     string username = entryInfo[0];
    6.  
    7.     if (username == playerName) {
    8.         int score = int.Parse(entryInfo[1]);
    9.         print(username + ": " + score);
    10.         return;
    11.       }
    12.   }
    13. }
    And call it like
    Code (CSharp):
    1. GetAndPrintPlayerScore("_WHATEVER_WAS_ALREADY_HERE_", "Bozo the Clown");
     
  6. heipp123

    heipp123

    Joined:
    Aug 1, 2015
    Posts:
    8
    Thanks