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
0
Question by Alismuffin · May 27, 2012 at 12:40 AM · stringfloatsplitting

Extracting list of floats from string C#

Hey guys!!

I have just run into something I haven't managed to get my head around. I have a string which contains a varying amount of floats which are varying lengths. I need to extract each float and place them in a List.

Here's an example:

This is the string:

[0.00442398712038994, 0.640771448612213, 0.830118358135223, 1.1674565076828, 1.52580320835114, 1.57927715778351]

I want it to be split up into as many number of floats as there are numbers here.

I only need help with splitting. I know how to put it in a list. The only reason I'm having trouble with this is the fact that there isn't really a consistent separation that I can see.

Usually I would use:

 string[] splitter = {"\", \""};
 
 string[] namesArray = input.Substring(2, input.Length-4).Split(splitter, System.StringSplitOptions.None);
 
 
 foreach (var name in namesArray) {
 
 string nameInput = nameStringJSON;
 
 string name = nameInput.Substring(55, nameInput.Length-57);
 
 print("name");
 
 }

Maybe there is some way of extracting floats and placing them in a list?

Comment
Add comment · Show 5
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 Eric5h5 · May 27, 2012 at 12:51 AM 0
Share

They're all separated by a comma? Seems consistent to me.

avatar image whydoidoit · May 27, 2012 at 01:08 AM 0
Share

Hmmm, I thought that. Are we missing something?!?

avatar image Alismuffin · May 27, 2012 at 01:17 AM 0
Share

I think maybe I'm just being a noob and missing something here. With the other ones they were separated from each other with "'s at the beginning and end which made it easy to split them, but here I have just commas at the end and also a [ at the beginning of the entire string and a ] at the end

But after writing this I realised I could have simply cut the [ ] off and split using the spaces as the beginning and ,'s as the ends

Ah sorry for that guys

avatar image whydoidoit · May 27, 2012 at 01:19 AM 1
Share

$$anonymous$$y answer below is pretty short for doing this, you could just make it:

 var myArray = inputTextString.Replace("[","").Replace("]","").Split(',').Select(Convert.ToSingle).ToArray();
avatar image whydoidoit · May 27, 2012 at 01:20 AM 1
Share

And don't worry about it - I've asked plenty of odd questions myself :)

1 Reply

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

Answer by whydoidoit · May 27, 2012 at 12:45 AM

Try this:

 using System.Linq;
 using System.Collections.Generic;

 var myList = inputTextString.Split(',').Select(Convert.ToSingle).ToList();

or

 var myArray = inputTextString.Split(',').Select(Convert.ToSingle).ToArray();
Comment
Add comment · Show 1 · 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 Alismuffin · May 27, 2012 at 01:26 AM 0
Share

Thanks a lot for your help!

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

6 People are following this question.

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

Related Questions

Input string is not in correct format 1 Answer

Store individual positions of GameObjects in a string 3 Answers

How to convert a float to a string and then have it appear in a gui rect 3 Answers

Make String Update for Value (GUI) 1 Answer

Unity Convert more int to one float or String and back? 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