- Home /
Removing the first line of a string?
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, "");
}
}
Answer by ArkaneX · Dec 16, 2013 at 04:37 PM
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);
Thanks a lot, I've implemented this and it works great!
Hmm, for some reason I needed to switch System.Environment.NewLine to '\n' and add a -1 after Length in the second line, it was deleting the wrong number of chars of the string...
Apologies for speaking from the future, but any reason why this bit of code works so slowly deleting letters individually? How can I get it to delete the line instantly?
I'm not sure what you're asking. The code in this answer does remove the first line. However it might not work for you if your line does not end with the System.Environment.NewLine character sequence. On windows this is usually "\r\n"
. On other platforms it may just be "\n"
.
If your string does not contain the sequence, index would be "-1" since the sequence could not be found. If you're on windows this code would simply remove the first character in this case because "-1 + 2 == 1". So it simply doesn't work when you search for the wrong character. The usage of System.Environment.NewLine
may not be the best choice here. If you know your text just contains "\n" you should do:
int index = oldText.IndexOf("\n");
newText = oldText.Substring(index + 1);
If you know your text contains "\r\n" you should do
int index = oldText.IndexOf("\r\n");
newText = oldText.Substring(index + 2);
Answer by Spoink · Dec 16, 2013 at 04:35 PM
What you would want is some type of separator which you can then identify and split into string-chunks whenever you find it.
private string mainString = "";
private void AddString(string newString)
{ this.mainString += ":" + newString; }
private string[] ExplodeMainString()
{ return this.mainString.Split(':'); ]
Answer by Spinnernicholas · Dec 16, 2013 at 04:36 PM
You will have to find the '\\n' character and then create a substring starting at it's position + 1.
Try this:
newText = oldText.Substring(oldText.IndexOf('\\n') + 1));
Not always \\n. Could be \\r\\n or even some other chars, depending on platform.
There is always a '\\n', even in "\\r\\n". I'm not too worried about leaving a carriage return. But, your answer is more accurate.
Answer by Dinkelborg · Dec 16, 2013 at 05:41 PM
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...
For all String methods see this link: http://msdn.microsoft.com/de-de/library/System.String_methods%28v=vs.110%29.aspx
And just another tip: ... the end of Line symbol under Windows is: \r\n and under Mac: \n Although \n works fine under Windows, \r\n will not work on Mac but you can just replace all instances of \r\n in your string by \n (through code) before you start working so it will be secure ...
--C#
C# on windows implicitly replaces '\\n' with '\\r\\n' under the hood. That's why '\\n' works in windows.