Julian Dormon
Julian Dormon

Reputation: 1779

C# How can I do a string replace method on the collection?

I have the following method which return a collection so I can then build a dropdown menu from in MVC. The problem is, I want to replace a substring within the values in the collection but I am not sure how to do so in C# (I'm a classic vb guy).

public class RolesManagement
{
    public static IEnumerable<string> BuildRoles(string DesiredRole)
    {
        var UserRoles = Roles.GetAllRoles();

        IEnumerable<string> roles;
        roles = UserRoles.Where(x => x.Contains("Sub")).Except(rejectAdmin);
        return roles;
    }

}

The above has been simplified for brevity. Each role in the roles collection looks like this: SubUser SubAdmin SubManager.

I simply want to return User Admin Manager

What would be the best approach for this in C# please?

My guess is that I have to do a foreach and replace the substring on each loop and the re-populate the value before moving to the next item.

If you could provide a code sample that would be great as I seem to still me tripping over syntax issues.

Much appreciated!

Upvotes: 1

Views: 2286

Answers (7)

spooti
spooti

Reputation: 247

this will return all items start with sub, with the sub removed

IEnumerable<string> = UserRoles .Where(x => x.StartsWith("sub")) .Select(y => y.Remove(0,3));

Upvotes: 1

Dustin Kingen
Dustin Kingen

Reputation: 21245

I would say if the roles are not going to change very often then make a mapping Dictionary.

public class RolesManagement
{
    private static readonly Dictionary<string,string> RoleMapping =
        new Dictionary<string,string>
        {
            {"SubUser", "User" },
            {"SubManager", "Manager" },
            {"SubAdmin", "Admin" }
        };

    public static IEnumerable<string> BuildRoles(string DesiredRole)
    {
        var UserRoles = Roles.GetAllRoles();

        IEnumerable<string> roles;
        roles = UserRoles.Where(x => x.Contains("Sub")).Except(rejectAdmin);
        return roles.Select(r => RoleMapping.ContainsKey(r) ? RoleMapping[r] : r);
    }
}

Or just store them inside the database as a FriendlyName column.

Upvotes: 1

Otaia
Otaia

Reputation: 451

You could do that with LINQ.

roles = UserRoles
    .Where(x => x.Contains("Sub"))
    .Except(rejectAdmin)
    .Select(x => x.Replace("Sub", ""));

Edit: Note that this method simply replaces all occurrences of the string "Sub" in all of the strings in roles. It's a very broad stroke and you may need to use a different lambda function if you only want to do the replacements you mentioned. See Romoku's post for help with that.

Upvotes: 2

svrcoder
svrcoder

Reputation: 96

Try this

List<string> x = new List<string>();
x.Add("SubUser");
x.Add("SubAdmin");
x.Add("SubManager");

var m = x.Select( y => y.Replace("Sub", ""));

Upvotes: 0

Sruti
Sruti

Reputation: 670

If I understand your problem right, you need to remove the occurence of "Sub" in a string. You can do additional checks if you have other conditions , like "Sub" is at the beginning of the string, case-insensitivity, etc.

One of the ways is to find the index of the string to be removed and then remove.

var s = "SubUser";
var t = s.Remove(s.IndexOf("Sub"), "Sub".Length);

Alternatively, you can also replace the string to be removed with an empty string.

var t = s.Replace("Sub", "");

Hope that helps.

Upvotes: 0

Heysem Katibi
Heysem Katibi

Reputation: 1906

public static IEnumerable<string> BuildRoles(string DesiredRole)
{
    var UserRoles = Roles.GetAllRoles();

    IEnumerable<string> roles;
    roles = UserRoles.Where(x => x.Contains("Sub")).Except(rejectAdmin).Select(x=>x.Replace("Sub","");
    return roles;
}

i supposed that x is string variable if not you have to do this replace on string collection

Upvotes: 0

user1801742
user1801742

Reputation: 21

public static IEnumerable<string> GetCorrectedRoleNames(IEnumerable<string> roles)
{
    List<string> correctedRoles = new List<string>();
    foreach (string role in roles)
        correctedRoles.Add(Regex.Replace(role, "Sub", string.Empty));
    return correctedRoles.AsEnumerable();
}

Upvotes: 0

Related Questions