Asynchronous
Asynchronous

Reputation: 3977

How to remove special characters from start of string but leave in the body of the string?

I have a very long string, I would like to remove keep only letters at the beginning:

Example: If the string starts like: 1234 I am going to the movies at 2pm: or !@#$% OMG I am 2 scared.

Then output should be: I am going to the movies at 2pm: And OMG I am 2 scared.

Note: The 2 still preserved.

In a nutshell I only want to remove the special character at the beginning of the string but leave all else in. I could use a Regex like this:

Regex.Replace(input, @"[^a-zA-Z]", "");

But not sure how I would incorporate this with s StartWith method?

By special character I am meaning only letters A-Z or a-z.

Thanks

Upvotes: 0

Views: 1348

Answers (3)

jcharlesworthuk
jcharlesworthuk

Reputation: 1079

I would go with TrimStart if you know the characters you want to remove. Or if all the characters are together at the start you could also do

myString.Substring(myString.IndexOf(' '));

to remove everything up until the first space

Upvotes: 0

Jan Mares
Jan Mares

Reputation: 805

This method of string may help: String.TrimStart

Upvotes: 0

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

var result = Regex.Replace(source, "^[^a-zA-Z]+", "");

^ at the beginning of regex pattern matches beginning of he string.

Upvotes: 4

Related Questions