Home > Software engineering >  Remove all chars up to last known char C#
Remove all chars up to last known char C#

Time:05-13

I have some string:

var s = "*%hello%my%name%is%Mike%HowAreYou";

The Mike%HowAreYou is changed from object to object.

I want to trim all the start up to the last % and get HowAreYou

How can I do that in the best way? Thanks

CodePudding user response:

s.Substring(s.LastIndexOf('%')   1)

CodePudding user response:

Shortest way I know, but requires "using System.Linq":

s.Split('%').Last();

The other answer is of course alright.

  • Related