Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim a string in c# after special character

Tags:

string

c#

asp.net

I want to trim a string after a special character..

Lets say the string is str="arjunmenon.uking". I want to get the characters after the . and ignore the rest. I.e the resultant string must be restr="uking".

like image 533
user3258052 Avatar asked Dec 07 '25 02:12

user3258052


2 Answers

How about:

string foo = str.EverythingAfter('.');

using:

public static string EverythingAfter(this string value, char c)
{
    if(string.IsNullOrEmpty(value)) return value;
    int idx = value.IndexOf(c);
    return idx < 0 ? "" : value.Substring(idx + 1);
}
like image 197
Marc Gravell Avatar answered Dec 08 '25 16:12

Marc Gravell


you can use like

string input = "arjunmenon.uking";
int index = input.LastIndexOf(".");
input = input.Substring(index+1, input.Split('.')[1].ToString().Length  );
like image 41
Anil Mahajan Avatar answered Dec 08 '25 15:12

Anil Mahajan



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!