• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 794
  • 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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