- Home /
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?
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.
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.
}
}
$$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);
}
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);
}
}
Yea, that would do the job. Just a correction. >= should be
Thank you! I have edited the answer and corrected the bug.
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
Follow this Question
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