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 AlucardJay · Mar 03, 2014 at 04:25 PM · stringlinqstring.split

Split String into groups of characters without using delimiters

The answer on this question by whydoidoit shows how to split a string into individual characters without using any delimiters, using LINQ.

Is it possible to split a string into groups of characters? For example, currently I am creating pairs of characters after splitting them like this :

 import System.Linq;
 
 function Foobar()
 {
     // split load string
     var stringSplit : String[] = guiString.Select( function(c) c.ToString() ).ToArray();
     var splitLength : int = stringSplit.Length;
     
     // put numbers into pairs, create int array
     var mapInfo : int[] = new int[ splitLength / 2 ];
     
     var i : int = 0;
     var index : int = 0;
     
     for ( i = 0; i < splitLength; i += 2 )
     {
         var stringPair : String = stringSplit[ i ] + stringSplit[ i + 1 ];
         mapInfo[ index ] = parseInt( stringPair );
         index ++;
     }
 }


Is there a way to split into groups of 2,3,4 etc using LINQ? 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

1 Reply

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

Answer by perchik · Mar 03, 2014 at 05:00 PM

From this stackoverflow question:

 var result = s.Select((x, i) => i)
               .Where(i => i % 4 == 0)
               .Select(i => s.Substring(i, s.Length - i >= 4 ? 4 : s.Length - i));

Just replace the 4s with whatever size you want.

Comment
Add comment · Show 11 · 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 AlucardJay · Mar 03, 2014 at 05:43 PM 0
Share

Thank you for such a quick response. However I really don't understand LINQ (have a hard time reading any of the $$anonymous$$SDN stuff), and am having problems implementing this in uJS.

Test Code :

 var stringSplit : String[] = guiString.Select((x, i) => i)
     .Where(i => i % 4 == 0)
     .Select(i => guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i));

Errors (7) :

 StringSplitTest.js(38,57): BCE0044: expecting ), found ','.
 StringSplitTest.js(38,59): BCE0044: expecting ), found 'i'.
 StringSplitTest.js(38,60): BCE0043: Unexpected token: ).
 StringSplitTest.js(38,65): BCE0043: Unexpected token: i.
 2 more Unexpected token for i, 1 more Unexpected token for )

I tried : declaring i as an int first, then in the Select bracket where i is first used, but those didn't work.

it seems to be breaking down after x, not expecting a 2-dimensional declaration (if I understand what is happening correctly). Any suggestions? Thanks again, sorry for the lack of understanding.

avatar image perchik · Mar 03, 2014 at 05:47 PM 0
Share

Ah, i wasn't paying attention. Forgot that Linq is also on Javascript now. $$anonymous$$y answer is in C#... Let me look at it for a $$anonymous$$ute.

avatar image whydoidoit · Mar 03, 2014 at 05:51 PM 2
Share

I did write that tutorial on Linq in JS and C# on http://unitygems.com - you need to use functions ins$$anonymous$$d of the lambdas. On iPad so can't type it all but basically

   (X,I) => I

Becomes

   function(x,I) { return I; }

Clearly without all of the stupid capital letters!

avatar image whydoidoit · Mar 03, 2014 at 05:59 PM 1
Share

$$anonymous$$e and @fafase for all the French / some of the AI - I think there's some of @alucardj's stuff on there, right aj? Anyone welcome to contibute though - I'm just the person with the hosting account.

avatar image ArkaneX · Apr 02, 2014 at 12:05 PM 1
Share

You get the error because Linq methods like Select or Where don't return array, but IEnumerable. In your case, IEnumerable is returned, and you can't assign it to variable of type String[].

To fix this, you either need to return an array, or to change your code to use IEnumerable. In first case, you can just add .ToArray() at the end of your main line of code:

 var stringSplit : String[] = guiString.Select( function(x,i) {return i;} ) 
     .Where( function(i) i % 4 == 0 )
     .Select( function(i) guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i) )
     .ToArray();

In second case, you can do something like this:

 var stringSplit = guiString.Select( function(x,i) {return i;} ) 
     .Where( function(i) i % 4 == 0 )
     .Select( function(i) guiString.Substring(i, guiString.Length - i >= 4 ? 4 : guiString.Length - i) );

 for (s in stringSplit)
 {
     Debug.Log(s);
 }
Show more comments

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

23 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 avatar image

Related Questions

LINQ Find String Duplicates in List 1 Answer

String.Split Not Splitting Correctly. Some Duplicates are being deleted. 1 Answer

Grab part of a string by looking for keyword 2 Answers

Error for 1st exactly same name var String 1 Answer

The method Resources.Load does not accept strings after splitting. 0 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