Solved

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

Posted on 2013-06-18
6
754 Views
Last Modified: 2013-06-25
Hi!

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 :)
0
Comment
Question by:webressurs
  • 3
  • 2
6 Comments
 
LVL 42

Assisted Solution

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

Open in new window

0
 
LVL 1

Author Comment

by:webressurs
ID: 39257514
Hi!

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

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.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 42

Expert Comment

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

Open in new window

0
 
LVL 1

Accepted Solution

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


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

Author Closing Comment

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

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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