I have a question that I hope someone can answer as I’ve spent quite some time with this now without being able to find an answer on the unity forums or answers.
I have a string that contains multiple lines, such as this (this is an example, the lines are dynamic);
This is Line 1
This is Line 2
This is Line 3
So, what I want to do with this is remove the top line. Is there a way to do this?? I’m thinking it should be something similar to this;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TextCleaner : MonoBehaviour
{
private dfLabel textLabel;
private string oldText;
private string newText;
private string result;
void Start()
{
textLabel = GetComponent<dfLabel>();
oldText = textLabel.Text; // This contains a few lines of text
newText = // This should be oldText, but with the top line trimmed away
result = oldText.Replace(newText, "");
}
}
Basically you have to find index of first new line character sequence (depends on platform, might be one char or two chars), and then you have to select all the text after this index + length of new line char sequence. Sample:
int index = oldText.IndexOf(System.Environment.NewLine);
newText = oldText.Substring(index + System.Environment.NewLine.Length);
You would normally just use the IndexOf Method to find the character that divides the lines (IndexOf) and than use CopyTo (CopyTo) to separate the first line if you want to keep it than you can use the Remove or Split method (Remove) (Split) to work further on an delete the first part…
But yeah this will require a specific character to be identified
PS: The ENDOFLINE symbol does exist and if your string is actually build up like this:
Line 1 (endOfLine)
Line 2 (endOfLine)
Line 3 (endOfLine)
and not like you mentioned (This is Line 1 This is Line 2 This is Line 3) than you can check and split … be aware to delete AFTER the identifier (so the endOfLine symbol for example) otherwise your line will start with a line break…
And just another tip: … the end of Line symbol under Windows is:
and under Mac:
Although
works fine under Windows,
will not work on Mac but you can just replace all instances of
in your string by
(through code) before you start working so it will be secure …