Replace carriage returns (Environment.NewLine) in start of string

Posted on 2013-06-18
Last Modified: 2013-06-25

If a string starts with one or multiple carriage returns (Environment.NewLine) I need to remove them. How can this be done? I don't want to remove the carriage returns INSIDE the string, just in the start of it.

Thanks :)
Question by:webressurs
  • 3
  • 2
LVL 42

Assisted Solution

sedgwick earned 500 total points
ID: 39256355
use TrimStart:
var trimStr = str.TrimStart(Environment.NewLine);

Open in new window


Author Comment

ID: 39257514

It seems like this don't work since .TrimStart() expect a char array. Environment.NewLine is not a single char.
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 39258458
define a new line \n or \r\n ?  0x0d,0x0a or just 0x0a ?

you have to do the trimstart 2x 1 for each single character and in case of multiple newlines recurse until it stops finding them.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

LVL 42

Expert Comment

ID: 39258465
var trimStr = str.TrimStart('\r', '\n');

Open in new window


Accepted Solution

webressurs earned 0 total points
ID: 39258589
I solved this, here is the solution:

char[] NewLineChars = Environment.NewLine.ToCharArray();
string trimStr = str.TrimStart(NewLineChars);

Author Closing Comment

ID: 39274142
I got the final solution based on sedgwick's comment.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DateTimepicker 4 33
Visual Studio 2015 Source Code Control 10 37
System.Speech 2 16
Ajax Panel Not Updating When Triggered By Timer 4 44
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now