Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
This question was closed Mar 25, 2014 at 08:31 AM by Fattie for the following reason:

Duplicate Question

avatar image
2
Question by FieseKuppe · Mar 25, 2014 at 08:01 AM · arraysstringsplit

How to split a string into array?

I try using the .Split function like this:

 var text = "this is a test bla bla bla this this this";
 var textSplit;
 
 function Start () {
 textSplit = text.Split(" "[0]);
 Debug.Log(textSplit[3]);
 }

If I do this Unity acts as I suspect it would by returning "test".

 Debug.Log(textSplit[4]);

If I run this or anything higher I get the error message IndexOutOfRangeException: index

 Debug.Log(textSplit.length);

Tells me 4

I can't find what the [0] is needed for. If I put anything else then 0 in there I get the error: IndexOutOfRangeException: Array index is out of range.

The following did return errors:

 textSplit = text.split(" ");
 textSplit = text.Split(" ",[0]);
 textSplit = text.Split(" ");


Is there another function or am I doing something wrong here?

Thanks for your help.

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

  • Sort: 
avatar image
5
Best Answer

Answer by fafase · Mar 25, 2014 at 08:18 AM

 var str = "A string with many words";
 var strArr:string[];
 
 function Start(){
    strArr= str.Split(" ");
 }  

I guess that should do it.

Comment
Add comment · 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
7

Answer by SirCrazyNugget · Mar 25, 2014 at 08:14 AM

Define textSplit as a string array:

 #pragma strict
 
 var text = "this is a test bla bla bla this this this";
 var textSplit : String[];
 
     function Start () {
         
         textSplit = text.Split(" "[0]);
         Debug.Log (text); //this is a test bla bla bla this this this
         Debug.Log (textSplit); //System.String[]
         
         for(var i : int = 0; i < textSplit.Length; i++){
             Debug.Log(textSplit[i]); //each split
         }
         Debug.Log (textSplit[1]); //is
         Debug.Log (textSplit[3]); //test
         
     }
     
Comment
Add comment · Show 3 · 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 FieseKuppe · Mar 25, 2014 at 08:17 AM 0
Share

Will test once I'm home :)

avatar image samf1111 · Jul 20, 2020 at 12:07 AM 0
Share

why the '[0]'? instead of the first character of a string, you should use a character instead by using single quotes. you can store characters in variables with 'char' like this:

char letter = 'a';

avatar image Bunny83 samf1111 · Jul 20, 2020 at 09:42 AM 0
Share

That's exactly the point, character literaly did not exist in UnityScript (which was Unity's javascript inspired dialect). It you needed a character literal you had to use a string and grab a single character from that string. Of course if the string only has one character you can only use the index 0. Like Fafase showed the Split method also has an overload that can split on strings rather than single chars which would work as well.


The original code should have worked just fine, I'm not sure why his array would only have 4 elements since his source text had more words in it. However maybe the "spaces" weren't normal spaces, who knows.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Split string from PlayePrefs 1 Answer

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

String split script 1 Answer

convert string to list of lists 1 Answer

Array of Array 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