1

I have the following code;

char[] leadingDot = { '.' };
string trimStart = fileName.TrimStart(leadingDot);

I cannot seem to figure out the syntax to combine it into a single line. ReSharper has no suggestions either.

I completely understand why the following code doesn't work, let alone look right, but I would expect something like:

string trimStart = fileName.TrimStart( { '.' } );

It gives me the same vibe you get when you type var x = null; Ideas?

5
  • 2
    Use new char[] { '.' } Commented Oct 18, 2019 at 20:00
  • facepalm. Didn't know char could be 'newed'. Commented Oct 18, 2019 at 20:01
  • @CarComp You're not making a new char, you're making a new char[]. Commented Oct 18, 2019 at 20:02
  • 1
    fileName.TrimStart('.'), filename.TrimStart('.', ':'), etc. There's an overload for one character, and an overload TrimStart(params char[] trimChars) that takes either an array of char, or any number of single char arguments which will be treated as an array internally. Commented Oct 18, 2019 at 20:02
  • 1
    To specifically address the question as it's stated in the title, you have to understand what is implied in the original context so that you can be explicit about it when the new context where that implication isn't possible. In this case, the implied part was new char[]. And then, in the new context, the syntax allows a different implication via the params keyword. Commented Oct 18, 2019 at 20:26

1 Answer 1

2
string trimStart = fileName.TrimStart(new char[] { '.'});
Sign up to request clarification or add additional context in comments.

1 Comment

I took it down to string trimStart = fileName.TrimStart('.')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.