Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2013-06-18
Medium Priority
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

by:Meir Rivkin
Meir Rivkin earned 2000 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 84

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.
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

LVL 42

Expert Comment

by:Meir Rivkin
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

927 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