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 Polinator · Feb 04, 2013 at 09:36 PM · splitting

How do you text.split a string with no delimiters/separators? (UnityScript)

Hello,

I have a string that I need to be split, so that each of the values can go into an array. Here is an example of it:

12345

67890

I tried using this code to put the values in an array.

 var myTextFile  :  TextAsset;
     
     function Awake(){
     for (var x = 0; x< myTextFile.text.Length; x++){
                         if (myTextFile.text[x] != myTextFile.text[5]) {
                             if(myTextFile.text[x] != "\n"){
                                 intArray.Push(myTextFile.text[x]);
                             }
                         }
                     }
     }

When reading the values, they are not integers. I did not parse them because I don't know how to split it without any delimiters. (i.e String.Split(""[0]) I always had to change it to put commas between the numbers and use "," as a separator. Is there a way I can get around this?

Something like this(found in another answer):

  var lineArray : String [] = myTextFile.text.Split("\n"[0]);
     for ( var thisLine : String in lineArray ) {
  
         var numberStrings : String [] = thisLine.Split(""[0]);
  
         for ( var thisNumber : String in numberStrings ) {
  
             var someInt : int = int.Parse(thisNumber);
  
             intArray.Push(someInt);
         }
     }

my workaround to this was to make the textFile toString. After that I just compared the string values to see if it matched.

 myStr = myTextFile.text[x].ToString();
     intArray.Push(myStr);

Not exactly an intArray anymore though. (stringArray)

Comment
Add comment · Show 2
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 robertbu · Feb 04, 2013 at 09:57 PM 0
Share

You are splitting a file on newlines? Here is my code(C#). There are probably more elegant solution.

 char[] archDelim = new char[] { '\r', '\n' };
 arstRawLines = stRawFile.Split(archDelim, StringSplitOptions.RemoveEmptyEntries);
avatar image Polinator · Feb 04, 2013 at 10:38 PM 0
Share

Not exactly, I am splitting it between each number AND the lines.

In the end I would want

floatArray[0] = 1,

floatArray[1] = 2,

floatArray[7] = 6,

floatArray[9] = 0,

something like that.

3 Replies

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

Answer by whydoidoit · Feb 04, 2013 at 10:45 PM

Try something like this?

  import System.Linq;

  ...

  var intArray = myTextFile.text.Where(function(c) c != "\n"[0]).Select( function(c) int.Parse(c.ToString())).ToArray();
Comment
Add comment · Show 7 · 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 whydoidoit · Feb 04, 2013 at 10:47 PM 0
Share

Not sure what that thing was about checking position 5 in the text array -> if you needed that then:

   var intArray = myTextFile.text.Where(function(c) c != '\n' && c != myTextFile.text[5]).Select( function(c) int.Parse(c)).ToArray();
avatar image Polinator · Feb 04, 2013 at 11:05 PM 0
Share

I tried using your code, and it says "no appropriate version of int.Parse for the argument list '(char)' was found."

I think I ran into this problem before, in that the text file I'm using is reading chars and not ints. That's why I had to do something like that second snippet of code I attached to make it into a string, and then parse the string. Correct me if I'm wrong.

The thing about checking position 5 was because I didn't know what was there, I was sure it was the newline "\n" but when checking for that, the statement wouldn't go through.

Apologies if I am wording this awkwardly, this is my first time on the forums.

avatar image Eric5h5 · Feb 05, 2013 at 12:04 AM 0
Share

@Polinator: use int.Parse(c.ToString()).

@$$anonymous$$ike: '\n' should be "\n"[0] in Unityscript.

avatar image whydoidoit · Feb 05, 2013 at 12:25 AM 0
Share

Oh yes - good point! Thanks Eric.

avatar image whydoidoit · Feb 05, 2013 at 05:35 PM 1
Share

You could try:

 var intArray = myTextFile.text.Where(function(c) char.IsDigit(c)).Select( function(c) int.Parse(c.ToString())).ToArray();
Show more comments
avatar image
0

Answer by hoy_smallfry · Feb 04, 2013 at 11:35 PM

You're trying to put each number digit in an array? "12345" -> {"1", "2", "3", "4", "5"} Correct?

To do that:

 var str : String = "12345";
 var strArray : String [] = str.Split("");
 var intArray : int [] = strArray.map(function(x){return parseInt(x)});

Hope that helps.

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 Polinator · Feb 05, 2013 at 05:35 PM 0
Share

Yes but I'm reading it from a text file, and str.Split(""); wouldn't work. Also I think it's

 var strArray : String [] = str.Split(""[0]);

for UnityScript, like Eric stated below in another answer. (With the [0])

avatar image
0

Answer by zenedee · Jul 07, 2014 at 09:04 AM

Find here more about c# string split ....String Split()

Zenee

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 whydoidoit · Jul 07, 2014 at 09:46 AM 0
Share

Wouldn't really help in this case given there are no delimiters

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

12 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

Related Questions

Questions about render distance in an open world game 0 Answers

parseInt a String not working 2 Answers

Cutting simple plane with a line (~2D) 2 Answers

split a scene into foreground and background in vr game. 0 Answers

Is it possible to round down 0.99 to 0? 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