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
1
Question by $$anonymous$$ · Dec 16, 2013 at 04:12 PM · stringtrim

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, "");
     }
 }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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);
Comment
Add comment · Show 5 · 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 $$anonymous$$ · Dec 16, 2013 at 05:52 PM 0
Share

Thanks a lot, I've implemented this and it works great!

avatar image Spinnernicholas · Dec 16, 2013 at 06:12 PM 0
Share

Simple, Fast, and Correct. Well deserved.(*).

avatar image Rodolfo-Rubens · Dec 07, 2015 at 03:27 AM 0
Share

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...

avatar image reikovaaa · Oct 27, 2020 at 12:25 AM 0
Share

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?

avatar image Bunny83 reikovaaa · Oct 27, 2020 at 01:40 AM 0
Share

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);
avatar image
0

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(':'); ]
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 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.

  • Find Character

  • Create Substring

Try this:

  newText = oldText.Substring(oldText.IndexOf('\\n') + 1));

Comment
Add comment · Show 2 · 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 ArkaneX · Dec 16, 2013 at 04:39 PM 0
Share

Not always \\n. Could be \\r\\n or even some other chars, depending on platform.

avatar image Spinnernicholas · Dec 16, 2013 at 04:45 PM 0
Share

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.

avatar image
0

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#

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 Spinnernicholas · Dec 16, 2013 at 07:20 PM 0
Share

C# on windows implicitly replaces '\\n' with '\\r\\n' under the hood. That's why '\\n' works in windows.

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

22 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

Related Questions

string.TrimEnd() is ignoring a character. 1 Answer

Variable in String 1 Answer

Is it a good idea to modify a "SendMessage" string during runtime? 1 Answer

GameObject to Variable to GameObject and add string? 1 Answer

Autocomplete, Monodevelop, and String/string 0 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