Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by oliver-jones · Jun 12, 2011 at 10:07 PM · arraystringfor-loopsplit

Splitting String Into Array

Hello,

I have string variable that holds a load of text, and I am trying to split it.

The string is basically player names, and player scores, it is displayed like this:

"player-score player-score player-score" --> "oliver-10 alex-20 tom-45"

And I want to break it out into arrays.

This is what I have so far (Start Function):

 var playerEntries : String[];
 var playerData : String[];
 var playerNamePull : String;
 var playerScorePull : int;
 
 playerEntries = formText.Split("\n"[0]);
 
 for(var entry : String in playerEntries){
     playerData = entry.Split("-"[0]);
    playerNamePull = playerData[0];
    playerScorePull = System.Convert.ToInt32(playerData[1]);
 }

Now, the playerEntries array works fine:

oliver-10

alex-20

tom-45

But nothing in the 'for' works - what am I doing wrong?

I'm trying to get it like this:

playerNamePull (string array) :

oliver

alex

tom

playerScorePull (int array) :

10

20

45

Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 13, 2011 at 02:51 AM

If you want the result to be an array you have to use arrays... :D

 var playerEntries : String[];
 var playerData : String[];
 var playerNamePull : String[];  // They are arrays now !
 var playerScorePull : int[];    //         ||
 
 function Start()
 {
     // Read in formText
     playerEntries = formText.Split("\n"[0]);
     playerNamePull = new String[playerEntries.Length];  // Create the arrays with the
     playerScorePull = new int[playerEntries.Length];    // same size as the playerEntries array
     
     // To work with multiple arrays we need an index, so I changed the for (each) into a "normal" for loop
     
     for(var i = 0; i < playerEntries.Length; i++){
         playerData = playerEntries[i].Split("-"[0]);
         playerNamePull[i] = playerData[0];
         playerScorePull[i] = System.Convert.ToInt32(playerData[1]);
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thewhiteaura · Jun 05, 2015 at 02:44 AM 1
Share

Thank you so much 4 year old answer! you helped prevent me from duplicating this question for an array I needed to split into 7 more arrays :D I wish I could give you a cookie. Now I just need to apply the filter option to remove the blank element at the end of the first array haha.

avatar image UnitySeller · Feb 01, 2018 at 06:55 PM 0
Share

Wow wrork amazing :D THAN$$anonymous$$ YOU

avatar image
0

Answer by Oliver Eberlei · Jun 12, 2011 at 10:28 PM

Remove the [0] from inside the Split function

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image oliver-jones · Jun 12, 2011 at 10:32 PM 0
Share

Now I get: No appropriate version of 'String.Split' for the argument list '(String)' was found

avatar image Oliver Eberlei · Jun 12, 2011 at 10:49 PM 0
Share

Hm, I just copy and pasted your code and it works fine. Does your PHP script deliver the right input?

This is the code i used to test

var playerEntries : String[];

var playerData : String[];

var playerNamePull : String;

var playerScorePull : int;

function Update() { var formText = "oliver-10\nalex-20\ntom-45";

 playerEntries = formText.Split("\n"[0]);

 for(var entry : String in playerEntries){
     playerData = entry.Split("-"[0]);
    playerNamePull = playerData[0];
    playerScorePull = System.Convert.ToInt32(playerData[1]);

    Debug.Log( playerNamePull + ": " + playerScorePull );
 }

}

avatar image oliver-jones · Jun 12, 2011 at 11:08 PM 0
Share

$$anonymous$$y PHP works fine: I have just noticed that in my playerEntries array, I get 3 extra elements that are empty. I also want playerNamePull, and playerScorePull to be arrays if possible.

avatar image oliver-jones · Jun 12, 2011 at 11:09 PM 0
Share

playerNamePull, and playerScorePull in the inspector appear to have nothing in it - although, it prints the names and scores

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

if statement not returning true 1 Answer

Splitting String only on a "space". Using my method --- FIXED 2 Answers

Check if specific object exist in list or array, Best Practices? 2 Answers

convert string to list of lists 1 Answer

How to convert a string to int array in Unity C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges