jre247
jre247

Reputation: 1897

How to remove slashes at beginning of string but not any in the middle

I'm trying to figure out how I can remove only the slashes (forward and backward) that occur at the beginning of the string below, up until the first letter or number. I don't want to remove any slashes in the middle of the string.

string: "\//hello\how\are/you"

looking for result like this: "hello\how\are/you"

Thanks! Jason

Upvotes: 1

Views: 105

Answers (2)

Ed Marty
Ed Marty

Reputation: 39700

string = string.TrimStart('\\', '/');

As an added bonus, you need not use a regex for this purpose.

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89639

You can use this pattern:

@"^[/\\]+"

It is a very basic pattern:

^ means start of the string

[/\\] is a character class that contains / and \ (note that you must escape the backslash to not escape the closing square bracket)

Upvotes: 4

Related Questions