Solved

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

Posted on 2013-06-18
6
745 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
Comment Utility
use TrimStart:
var trimStr = str.TrimStart(Environment.NewLine);

Open in new window

0
 
LVL 1

Author Comment

by:webressurs
Comment Utility
Hi!

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

Expert Comment

by:David Johnson, CD, MVP
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 42

Expert Comment

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

Open in new window

0
 
LVL 1

Accepted Solution

by:
webressurs earned 0 total points
Comment Utility
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
Comment Utility
I got the final solution based on sedgwick's comment.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

7 Experts available now in Live!

Get 1:1 Help Now