user2207102
user2207102

Reputation: 35

Remove Char after last special char

Hi I have a string maybe like this

string str="user1,user2,user3,user4,user5,user6";

I want remove all of string at last , then the str became user6. How can I do it use c#

Upvotes: 1

Views: 61

Answers (2)

Roman Marusyk
Roman Marusyk

Reputation: 24619

Try:

string newStr = str.Substring(str.LastIndexOf(",") + 1));

or add using System.Linq; and use:

string newStr = str.Split(',').Last();

Upvotes: 4

Codor
Codor

Reputation: 17605

It could be done using Linq as follows.

string Result
    = str.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries).Last();

Upvotes: 0

Related Questions