Solved

Help with C# string literal

Posted on 2006-11-06
5
2,141 Views
Last Modified: 2008-01-09
Can anybody give me a quick run down of what this does;


header[header.Count - 1] += oneLine.TrimStart(new char[] {' ' /* dangerous ! */, '\t'});

I'm trying to convert an implementation of domainKeys to vb.net and I have no idea what is going on here. I get that on eof the param array is a tab - but what is going on before that?

It's only the TrimStart call I don't understand. What is going on?
0
Comment
Question by:thefritterfatboy
5 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17881545
The string stored in the last position in the array called header is having another string (oneLine) appended to it but only after any leading spaces or tabs have been trimmed off it.

TrimStart() will remove whitespace characters from the start of a string.
TrimStart(char[]) will remove any characters in the char[].
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 200 total points
ID: 17881553
for some reason someone has decided that trimming leading spaces is dangerous and had addeed a comment to that effect.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17881574
TrimStart(new char[] {' ', '\t'});

Result is string instance without leading spaces and tabs in original string. This means, if oneLine string contains any number of spaces and tabs in any order in the beginning, they are removed from result. BTW, I don't see anything dangerous here. For example , if oneLine is "   abc", this is executed as:

header[header.Count - 1] += "abc";

0
 
LVL 13

Expert Comment

by:joechina
ID: 17881625
Difference between
TrimStart(new char[] {' ', '\t'});
and
Trim();

is

Trim will remove new line and return as well

Try

string s = @"



aaa";

Console.WriteLine(s.Trim());

Console.WriteLine(s.TrimStart(new char[] {' ', '\t'}));

You will see the difference.

0
 
LVL 10

Author Comment

by:thefritterfatboy
ID: 17882305
It was the comment that had me there. I wasn't aware C# allowed for inline comments.

... I suspect the "danger" is to do with how the string is used in the encryption further down the script.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now