• 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 KitoCode · Apr 17, 2015 at 04:12 AM · arraystringsplit

Splitting String only on a "space". Using my method --- FIXED

Good afternoon all,

I am trying to create a RPG style text, and I wish to split up a single string given in and have it split into an array based on how many characters are allowed in each array index. Here is the code I have been working on.

 float remainder = newText.Length % splitIndex;
 _textBlocks = new string[(int)(Mathf.Floor(newText.Length / (float)splitIndex)) + ((remainder==0) ? 0 : 1)];
 for(int x = 0; x < _textBlocks.Length; x++)
 {
     _textBlocks[x] = newText.Substring(x*splitIndex, (int)((x==_textBlocks.Length-1) ? remainder : splitIndex));
 }

The above code does what I wish: The Issue IS It splits in the middle of words depending on the string passed in.

I was t$$anonymous$$nking of w$$anonymous$$le looping backward until it $$anonymous$$ts an empty string then add that array to the _textBlocks[x]. However the methods I am trying yield no good result, or gives more issues than it solves.

OTHER THEORY:

Split the entire string via. string.split(" "[0]) then count each array length, add it to clump value, if that value exceeds splitIndex, then add string.split values that include what we just looked at... Confusing, a bit. Hopfully someone can help me make sense of t$$anonymous$$s.

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

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

Answer by KitoCode · Apr 17, 2015 at 11:57 PM

FIXED, I ended up looping through the arrays after they were built and fixing them accordingly.

 float remainder = newText.Length % splitIndex;
 _textBlocks = new string[(int)(Mathf.Floor(newText.Length / (float)splitIndex)) + ((remainder==0) ? 0 : 1)];
 
 for(int x = 0; x < _textBlocks.Length; x++)
 {
     _textBlocks[x] = newText.Substring(x*splitIndex, (int)((x==_textBlocks.Length-1) ? ((remainder==0) ? splitIndex : remainder) : splitIndex));
 }
 
 for(int x = 0; x < _textBlocks.Length; x++)
 {
     w$$anonymous$$le(_textBlocks[x].Substring(_textBlocks[x].Length - 1) != " ")
     {
         if(x+1 != _textBlocks.Length)
         {
             _textBlocks[x+1] = _textBlocks[x+1].Insert(0, _textBlocks[x].Substring(_textBlocks[x].Length - 1));
             _textBlocks[x] = _textBlocks[x].Remove(_textBlocks[x].Length - 1);
         }
         else
             break;
     }
 }


YAY :D Finally I can move on!

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
0

Answer by Hrungdak · Apr 17, 2015 at 05:33 AM

It would be helpful if you post an input-string and the result that you want to have. The code above splits the text with substring. There is no code to deal with words or spaces. At least i can't see one.

If you want to display text with a certain width (say 20 characters):

  - Split the text after each word (with split, as you said)
  - take the first word as actual string
  - A: take the next word
  - if actual string + space + next word is longer than splitIndex put actual string in array, the next word is the new first word
 - if not, bind the next word to the actual string
  - redo from A: until no more words
 




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 KitoCode · Apr 17, 2015 at 09:47 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

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

convert string to list of lists 1 Answer

Change part of a string [Solved] 3 Answers

Strings, Arrays and Split in js 1 Answer

Splitting 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