• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 801
  • Last Modified:

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

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
webressurs
Asked:
webressurs
  • 3
  • 2
2 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
use TrimStart:
var trimStr = str.TrimStart(Environment.NewLine);

Open in new window

0
 
webressursAuthor Commented:
Hi!

It seems like this don't work since .TrimStart() expect a char array. Environment.NewLine is not a single char.
0
 
David Johnson, CD, MVPOwnerCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Meir RivkinFull stack Software EngineerCommented:
var trimStr = str.TrimStart('\r', '\n');

Open in new window

0
 
webressursAuthor Commented:
I solved this, here is the solution:


char[] NewLineChars = Environment.NewLine.ToCharArray();
string trimStr = str.TrimStart(NewLineChars);
0
 
webressursAuthor Commented:
I got the final solution based on sedgwick's comment.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now