Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by ina · Dec 25, 2010 at 07:01 AM · stringexplosionsplit

Unity JS Equivalent of split()?

It looks like Unity does not support Javascript's String.split() (or PHP's explode())... Is there an equivalent of this?

How would you convert a delimited string to an array? This would be useful for loading level data stored in an external server.

Comment
Add comment · Show 1
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 ina · Dec 25, 2010 at 07:20 AM 0
Share

http://answers.unity3d.com/questions/24539/what-is-the-0-in-string-split explains the syntax for why there is a [0]

2 Replies

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

Answer by Mike 3 · Dec 25, 2010 at 07:03 AM

You use the .NET one ( http://msdn.microsoft.com/en-us/library/system.string.split.aspx )

yourString.Split(","[0]);

The [0] is a dodgy javascript hack to get the char from the string (as you can't do a char literal)

Comment
Add comment · Show 5 · 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 ina · Dec 25, 2010 at 07:09 AM 0
Share

so, for a string str separated by ; it'd be str.Split(";"[0]); ? just tried arr=new Array(); arr= str.Split(";"[0]); Debug.Log(arr[0].ToString()); nothing is reading up in Debug.Log(arr[0]);

avatar image Mike 3 · Dec 25, 2010 at 07:14 AM 0
Share

It returns String[] not Array. if you just do var arr = str.Split(";"[0]); Debug.Log(arr[0]); it should work

avatar image ina · Dec 25, 2010 at 07:21 AM 0
Share

it looks like either var arr:Array; arr=new Array(); or var arr:String[]; works - this seems like a temporary hack though, do you know if it'd be deprecated?

avatar image fireDude67 · Dec 25, 2010 at 07:42 AM 0
Share

If I were you, I would use C# for this exact purpose, greater power and the entire $$anonymous$$ono/.NET library

avatar image Mike 3 · Dec 25, 2010 at 07:49 AM 0
Share

Oh right - javascript probably just does implicit conversions for native arrays to Array. Shouldn't be deprecated. And, uh, you can use pretty much the whole of .NET with js fireDude o.o

avatar image
1

Answer by Chris 21 · Jan 03, 2011 at 11:31 AM

same question, I've yet to find a working solution without giving a format error when attempting to split an array in javascript.

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 Mike 3 · Jan 03, 2011 at 11:42 AM 0
Share

You should put this in a new question so it's easier to deal with - add in the code you're trying and the exact exception it's throwing

avatar image Chris 21 · Jan 03, 2011 at 12:15 PM 0
Share

ah sure, I suppose this will be moreso a fair warning then. I was using PlayerPrefs to write some Vector3/Quaternion's to file using the ToString() method and made a blind assumption it wouldn't append anything to the written data. rather it writes parenthesis and commas to delineate the components which were giving a nonsensical error in unrelated to the formatting.

avatar image Chris 21 · Jan 03, 2011 at 12:15 PM 1
Share

anyways the quick solution was:

str = PlayerPrefs.GetString("player.position"); str = Regex.Replace(str,"[(),]",""); splitter = str.Split(" " [0]); PlayerStateHandler.gPlayer.transform.position = Vector3(parseFloat(splitter[0]), parseFloat(splitter[1]),parseFloat(splitter[2]));

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

No one has followed this question yet.

Related Questions

StringSplitOptions.RemoveEmptyEntries - Unknown identifier 2 Answers

issues splitting strings 1 Answer

Extract number from string? 3 Answers

Split a string every 'n' characters? 5 Answers

How to split a string into array? 2 Answers


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