Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Rukas90 · Oct 11, 2017 at 09:53 AM · meshtextstringstringsstring.split

Problem splitting up strings

Hello everyone!

I'm making a console for my game and I recently faced one problem. I added lot's of information to the string and then displayed that information inside UI text. The problem is that the string became too long for TextMeshGenerator. So I came up with some solution. To set maximum characters for each string and when the string reaches that maximum amount create new UI text. The issues is that how do I split / cut the string? For example I need to add 1000 characters to the string so I, add for example 800 of characters until the first string is full (reached max amount of characters) and I add the rest 200 of the characters to the new created string.
Can someone give me some tips on how to achieve this? Thank you!

Also how much characters UI text / string can contain, until it starts to give errors?

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
2
Best Answer

Answer by Dragate · Oct 11, 2017 at 10:17 AM

You could use string.Substring(int, index, int length).

 string string1 = stringToSplit.Substring(0,800); 
 //will get the first 800 characters
 
 string string2 = stringToSplit.Substring(800,200); 
 //will get 200 characters starting from the 800th character

You could even make a method that splits your string and returns an array/list of strings. I don't know what's the cap of UI text in characters.

Comment
Add comment · Show 3 · 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 Rukas90 · Oct 12, 2017 at 05:39 PM 0
Share

Thank you for the reply. :) I tried implementing this and it worked fine, until I ran into other problems.. This method cuts the next in the middle of the sentence and it make it look really weird.. Also I use Tag's in my text, like "color" and when it cuts the tag the color is not applied and the tag is also showing, so it's not good..

In my script I display text in UI text and whenever the ui text string reaches the max amount of characters I Instantiate new text and I add the text to the list.

Also this is the script that add's the text to the console:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityStandardAssets.ImageEffects;
 using System.Collections.Generic;
 
 public class Console$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public static Console$$anonymous$$anager instance;
     ConsoleDataReciever info = new ConsoleDataReciever();
 
     public bool useConsole;
 
     [SerializeField] private InputField inputField;
     [SerializeField] private RectTransform Console$$anonymous$$essage;
 
     [TextArea (2,2)]
     [SerializeField] private string lineBreak;
     private string _ConsoleContent; // info
     public int max = 65000; // $$anonymous$$aximum length of the string
 
     [SerializeField] private Text container;
     [SerializeField] private RectTransform newContainer;
     [SerializeField] private RectTransform holder;
 
     void Awake ()
     {
         instance = this;
     }
     
     void Update ()
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypadEnter))
         {
             SubmitData();
         }
     }
 
     public void SubmitData ()
     {
         ExecuteCommand.instance.Send$$anonymous$$essage(inputField.text.ToString(), info, Send$$anonymous$$essageOptions.DontRequireReceiver);
         if (inputField.text != "")
             if(!info.dataRecieved)
             {
                 WriteConsole$$anonymous$$essage.instance.Log(inputField.text.ToString(), "", LogType.Log);
             }
         reset();
     }
 
     public void AddConsole$$anonymous$$essage (string data)
     {
         if (data == "") return; // Check if new message is not an empty string, if so return from this function.
 
         string new$$anonymous$$essage = lineBreak + data + lineBreak + "</color>"; // Create complete new message string.
 
         _ConsoleContent += new$$anonymous$$essage; // Add new text to content string.
         container.text = _ConsoleContent; // Display in text.
     }
 }
 
avatar image Dragate Rukas90 · Oct 13, 2017 at 08:46 AM 0
Share

$$anonymous$$odifying TBART82's loop to hopefully suit your needs:

 for (int i = 0; i < info.Length; i += maxStringLength){
       // Split the string into Substrings
       string split = info.Substring(i, $$anonymous$$athf.$$anonymous$$in(maxStringLength, info.Length - i));
 
       // If the occurences of < & > are not equal it means the last tag is not whole
       if(split.Split('<').Length != split.Split('>').Length){
           //Get the last index of < to exclude from this split
           int index = split.LastIndexOf('<');
           // Re-assign the split from start of split till char before the last <
           split = split.Substring(0,index);
           // Re-assign i since we didn't cut at maxStringLenth, but at index
           i = i - maxStringLength + index;
       }
 
       // Add the string to the 'splitStrings' list.
       splitStrings.Add(split);
 }
avatar image TBART82 Dragate · Oct 14, 2017 at 02:04 AM 0
Share

Yep, that will do the trick!

avatar image
1

Answer by TBART82 · Oct 11, 2017 at 10:56 AM

I came up with a way to procedurally split up strings easily. Hope this solution will work for you!

     // The entire string which you want to use
     public string info;
     // The maximum length of the string
     public int maxStringLength = 800;
 
     // A list to hold all of the split strings
     public List<string> splitStrings;
 
     private void Start()
     {
         // Iterate throught the entire strings length.
         for (int i = 0; i < info.Length; i += maxStringLength)
         {
             // Split the string into Substrings
             string split = (info.Substring(i, Mathf.Min(maxStringLength, info.Length - i)));
             // Add the string to the 'splitStrings' list.
             splitStrings.Add(split);
         }
     }
Comment
Add comment · Show 3 · 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 Dragate · Oct 11, 2017 at 11:09 AM 0
Share

Yea, that would do the job. Just a correction. >= should be

avatar image TBART82 Dragate · Oct 11, 2017 at 11:52 AM 0
Share

Thank you! I have edited the answer and corrected the bug.

avatar image Rukas90 · Oct 12, 2017 at 06:01 PM 0
Share

Thank your for the reply! :) I tried to implement this, but didn't really had much luck.. I'm also trying to avoid splitting up strings in the middle of the sentence. I'm also using tags in the text, like color tags. If it's no trouble could you take a look at my script?

 using UnityEngine;
 using UnityEngine.UI;
 using UnityStandardAssets.ImageEffects;
 using System.Collections.Generic;
 
 public class Console$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public static Console$$anonymous$$anager instance;
     ConsoleDataReciever info = new ConsoleDataReciever();
 
     public bool useConsole;
 
     [SerializeField] private InputField inputField;
     [SerializeField] private RectTransform Console$$anonymous$$essage;
 
     [TextArea (2,2)]
     [SerializeField] private string lineBreak;
     private string _ConsoleContent; // info
     public int max = 65000; // $$anonymous$$aximum length of the string
 
     [SerializeField] private Text container;
     [SerializeField] private RectTransform newContainer;
     [SerializeField] private RectTransform holder;
 
     void Awake ()
     {
         instance = this;
     }
     
     void Update ()
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$eypadEnter))
         {
             SubmitData();
         }
     }
 
     public void SubmitData ()
     {
         ExecuteCommand.instance.Send$$anonymous$$essage(inputField.text.ToString(), info, Send$$anonymous$$essageOptions.DontRequireReceiver);
         if (inputField.text != "")
             if(!info.dataRecieved)
             {
                 WriteConsole$$anonymous$$essage.instance.Log(inputField.text.ToString(), "", LogType.Log);
             }
         reset();
     }
 
     public void AddConsole$$anonymous$$essage (string data)
     {
         if (data == "") return; // Check if new message is not an empty string, if so return from this function.
 
         string new$$anonymous$$essage = lineBreak + data + lineBreak + "</color>"; // Create complete new message string.
 
         _ConsoleContent += new$$anonymous$$essage; // Add new text to content string.
         container.text = _ConsoleContent; // Display in text.
     }
 }
 

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

82 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 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 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 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 loop through char in a string to find tags 0 Answers

String.Remove not working 1 Answer

Grab part of a string by looking for keyword 2 Answers

Litjson autoformat 1 Answer

How to replace this Gui text with text mesh please ? 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