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 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 thinking of while looping backward until it hits 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 this.

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++)
 {
     while(_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

The idea of this method is that you can add any string and it will sort out the array, no matter how big of the string, it will break it up accordingly.

http://pastebin.com/yqWn1wy$$anonymous$$

That is what I came up with, almost complete, if this method leaves me to more issues I believe I will take your answer.

FOR EXA$$anonymous$$PLE:

string inputString = Hey there Hrungdak, my name is IcarusOtaku. I love to make games. Do you have any examples of your work?

string textblocks[] = new string[unknownLength]; int splitIndex = 30; //Just a test number

textblocks[0] = "Hey there Hrungdak, my name is"; textblocks[1] = " IcarusOtaku. I love to make "; textblocks[2] = "games. Do you have any exampl"; textblocks[3] = "es of your work?";

///Below is the desired out come. textblocks[0] = "Hey there Hrungdak, my name is"; textblocks[1] = " IcarusOtaku. I love to make "; textblocks[2] = "games. Do you have any "; textblocks[3] = "examples of your work?";

NOTICE: Textblock[2] is now missing examples because we put it in textblock[3].

Bottom line, I do not wish for a block to have a chopped up word, so i assumed i would make sure the block ends with a ' '.

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

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