Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove DateTime substring from string

I have a bunch of strings, some of which have one of the following formats:

"TestA (3/12/10)"
"TestB (10/12/10)"

The DateTime portion of the strings will always be in mm/dd/yy format.

What I want to do is remove the whole DateTime part including the parenthesis. If it was always the same length I would just get the index of / and subtract that by the number of characters up to and including the (. But since the mm portion of the string could be one or two characters, I can't do that.

So is there a way to do a .Contains or something to see if the string contains the specified DateTime format?

like image 200
pfinferno Avatar asked Mar 10 '26 05:03

pfinferno


1 Answers

You could use a Regular Expression to strip out the possible date portions if you can be sure they would consistently be in a certain format using the Regex.Replace() method :

var updatedDate = Regex.Replace(yourDate,@"\(\d{1,2}\/\d{1,2}\/\d{1,2}\)","");

You can see a working example of it here, which yields the following output :

TestA (3/12/10)  > TestA
TestB (10/12/10) > TestB
TestD (4/5/15)   > TestC
TestD (4/6/15)   > TestD
like image 164
Rion Williams Avatar answered Mar 11 '26 18:03

Rion Williams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!